Skip to content

Commit 92ba0f6

Browse files
committed
update Java version
1 parent a91eb22 commit 92ba0f6

File tree

9 files changed

+336
-152
lines changed

9 files changed

+336
-152
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.upokecenter</groupId>
55
<artifactId>cbor</artifactId>
66
<packaging>jar</packaging>
7-
<version>4.4.1</version>
7+
<version>4.4.1-SNAPSHOT</version>
88
<name>CBOR (Concise Binary Object Representation)</name>
99
<description>A Java implementation of Concise Binary Object Representation (CBOR), a general-purpose binary data format defined in RFC 8949.</description>
1010
<url>https://github.com/peteroupc/CBOR-Java</url>

src/main/java/com/upokecenter/cbor/CBORJson.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -598,22 +598,24 @@ private CBORObject ParseJSONObject(int depth) {
598598
// constructor directly
599599
obj = CBORObject.FromRaw(this.NextJSONString());
600600
key = obj;
601-
if (!this.options.getAllowDuplicateKeys() &&
602-
myHashMap.containsKey(obj)) {
603-
this.RaiseError("Key already exists: " + key);
604-
return null;
605-
}
606601
break;
607602
}
608603
}
609604
if (this.SkipWhitespaceJSON() != ':') {
610605
this.RaiseError("Expected a ':' after a key");
611606
}
607+
int oldCount = myHashMap.size();
612608
// NOTE: Will overwrite existing value
613609
myHashMap.put(key, this.NextJSONValue(
614610
this.SkipWhitespaceJSON(),
615611
nextChar,
616612
depth));
613+
int newCount = myHashMap.size();
614+
if (!this.options.getAllowDuplicateKeys() &&
615+
oldCount == newCount) {
616+
this.RaiseError("Duplicate key already exists");
617+
return null;
618+
}
617619
switch (nextChar[0]) {
618620
case ',':
619621
seenComma = true;

src/main/java/com/upokecenter/cbor/CBORJson2.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -621,22 +621,24 @@ private CBORObject ParseJSONObject(int depth) {
621621
// constructor directly
622622
obj = CBORObject.FromRawUtf8(this.NextJSONString());
623623
key = obj;
624-
if (!this.options.getAllowDuplicateKeys() &&
625-
myHashMap.containsKey(obj)) {
626-
this.RaiseError("Key already exists: " + key);
627-
return null;
628-
}
629624
break;
630625
}
631626
}
632627
if (this.SkipWhitespaceJSON() != ':') {
633628
this.RaiseError("Expected a ':' after a key");
634629
}
630+
int oldCount = myHashMap.size();
635631
// NOTE: Will overwrite existing value
636632
myHashMap.put(key, this.NextJSONValue(
637633
this.SkipWhitespaceJSON(),
638634
nextchar,
639635
depth));
636+
int newCount = myHashMap.size();
637+
if (!this.options.getAllowDuplicateKeys() &&
638+
oldCount == newCount) {
639+
this.RaiseError("Duplicate key already exists");
640+
return null;
641+
}
640642
switch (nextchar[0]) {
641643
case ',':
642644
seenComma = true;

src/main/java/com/upokecenter/cbor/CBORJson3.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -525,22 +525,24 @@ private CBORObject ParseJSONObject(int depth) {
525525
// constructor directly
526526
obj = CBORObject.FromRaw(this.NextJSONString());
527527
key = obj;
528-
if (!this.options.getAllowDuplicateKeys() &&
529-
myHashMap.containsKey(obj)) {
530-
this.RaiseError("Key already exists: " + key);
531-
return null;
532-
}
533528
break;
534529
}
535530
}
536531
if (this.SkipWhitespaceJSON() != ':') {
537532
this.RaiseError("Expected a ':' after a key");
538533
}
534+
int oldCount = myHashMap.size();
539535
// NOTE: Will overwrite existing value
540536
myHashMap.put(key, this.NextJSONValue(
541537
this.SkipWhitespaceJSON(),
542538
nextchar,
543539
depth));
540+
int newCount = myHashMap.size();
541+
if (!this.options.getAllowDuplicateKeys() &&
542+
oldCount == newCount) {
543+
this.RaiseError("Duplicate key already exists");
544+
return null;
545+
}
544546
switch (nextchar[0]) {
545547
case ',':
546548
seenComma = true;

src/main/java/com/upokecenter/cbor/CBORObject.java

Lines changed: 54 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ licensed under Creative Commons Zero (CC0):
2020
// for ReadJSON, FromJSONString, FromJSONBytes
2121
// TODO: In Java version add overloads for Class<T> in overloads
2222
// that take java.lang.reflect.getType()
23-
// TODO: Add TryGetValue method
2423

2524
/**
2625
* <p>Represents an object in Concise Binary Object Representation (CBOR) and
@@ -166,6 +165,7 @@ private static CBORObject ConstructIntegerValue(int v) {
166165
private static final int CBORObjectTypeSimpleValue = 7;
167166
private static final int CBORObjectTypeDouble = 8;
168167
private static final int CBORObjectTypeTextStringUtf8 = 9;
168+
private static final int CBORObjectTypeTextStringAscii = 10;
169169

170170
private static final int StreamedStringBufferLength = 4096;
171171

@@ -480,6 +480,7 @@ public final CBORType getType() {
480480
return CBORType.ByteString;
481481
case CBORObjectTypeTextString:
482482
case CBORObjectTypeTextStringUtf8:
483+
case CBORObjectTypeTextStringAscii:
483484
return CBORType.TextString;
484485
default: throw new IllegalStateException("Unexpected data type");
485486
}
@@ -645,7 +646,7 @@ public CBORObject GetOrDefault(Object key, CBORObject defaultValue) {
645646
if (this.getType() == CBORType.Map) {
646647
Map<CBORObject, CBORObject> map = this.AsMap();
647648
CBORObject ckey = CBORObject.FromObject(key);
648-
return (!map.containsKey(ckey)) ? defaultValue : map.get(ckey);
649+
return PropertyMap.GetOrDefault(map, ckey, defaultValue);
649650
}
650651
return defaultValue;
651652
}
@@ -1932,6 +1933,11 @@ private long CalcEncodedSize(int depth) {
19321933
size = (size + IntegerByteLength(bytes.length));
19331934
return size + bytes.length;
19341935
}
1936+
if (cbor.getItemType() == CBORObjectTypeTextStringAscii) {
1937+
String str = (String)this.getThisItem();
1938+
size = (size + IntegerByteLength(str.length()));
1939+
return size + str.length();
1940+
}
19351941
switch (cbor.getType()) {
19361942
case Integer: {
19371943
if (cbor.CanValueFitInInt64()) {
@@ -2200,11 +2206,14 @@ public static CBORObject FromObject(String strValue) {
22002206
if (strValue.length() == 0) {
22012207
return GetFixedObject(0x60);
22022208
}
2203-
if (DataUtilities.GetUtf8Length(strValue, false) < 0) {
2209+
long utf8Length = DataUtilities.GetUtf8Length(strValue, false);
2210+
if (utf8Length < 0) {
22042211
throw new IllegalArgumentException("String contains an unpaired " +
22052212
"surrogate code point.");
22062213
}
2207-
return new CBORObject(CBORObjectTypeTextString, strValue);
2214+
return new CBORObject(
2215+
strValue.length() == utf8Length ? CBORObjectTypeTextStringAscii : CBORObjectTypeTextString,
2216+
strValue);
22082217
}
22092218

22102219
/**
@@ -4592,7 +4601,8 @@ public float AsSingle() {
45924601
public String AsString() {
45934602
int type = this.getItemType();
45944603
switch (type) {
4595-
case CBORObjectTypeTextString: {
4604+
case CBORObjectTypeTextString:
4605+
case CBORObjectTypeTextStringAscii: {
45964606
return (String)this.getThisItem();
45974607
}
45984608
case CBORObjectTypeTextStringUtf8: {
@@ -4801,6 +4811,15 @@ public int compareTo(CBORObject other) {
48014811
(byte[])objB);
48024812
break;
48034813
}
4814+
case CBORObjectTypeTextStringAscii: {
4815+
String strA = (String)objA;
4816+
String strB = (String)objB;
4817+
int alen = strA.length();
4818+
int blen = strB.length();
4819+
cmp = (alen < blen) ? (-1) : ((alen > blen) ? 1 :
4820+
strA.compareTo(strB));
4821+
break;
4822+
}
48044823
case CBORObjectTypeTextString: {
48054824
String strA = (String)objA;
48064825
String strB = (String)objB;
@@ -4847,20 +4866,37 @@ public int compareTo(CBORObject other) {
48474866
cmp = CBORUtilities.ByteArrayCompare(
48484867
this.EncodeToBytes(),
48494868
other.EncodeToBytes());
4850-
} else if (typeB == CBORObjectTypeTextString && typeA ==
4869+
} else if ((typeB == CBORObjectTypeTextString || typeB ==
4870+
CBORObjectTypeTextStringAscii) && typeA ==
48514871
CBORObjectTypeTextStringUtf8) {
48524872
cmp = -CBORUtilities.CompareUtf16Utf8LengthFirst(
48534873
(String)objB,
48544874
(byte[])objA);
4855-
} else if (typeA == CBORObjectTypeTextString && typeB ==
4875+
} else if ((typeA == CBORObjectTypeTextString || typeA ==
4876+
CBORObjectTypeTextStringAscii) && typeB ==
4877+
CBORObjectTypeTextStringUtf8) {
4878+
cmp = CBORUtilities.CompareUtf16Utf8LengthFirst(
4879+
(String)objA,
4880+
(byte[])objB);
4881+
} else if ((typeA == CBORObjectTypeTextString && typeB ==
4882+
CBORObjectTypeTextStringAscii) ||
4883+
(typeB == CBORObjectTypeTextString && typeA ==
4884+
CBORObjectTypeTextStringAscii)) {
4885+
cmp = -CBORUtilities.CompareStringsAsUtf8LengthFirst(
4886+
(String)objB,
4887+
(String)objA);
4888+
} else if ((typeA == CBORObjectTypeTextString || typeA ==
4889+
CBORObjectTypeTextStringAscii) && typeB ==
48564890
CBORObjectTypeTextStringUtf8) {
48574891
cmp = CBORUtilities.CompareUtf16Utf8LengthFirst(
48584892
(String)objA,
48594893
(byte[])objB);
48604894
} else {
4861-
int ta = (typeA == CBORObjectTypeTextStringUtf8) ?
4895+
int ta = (typeA == CBORObjectTypeTextStringUtf8 || typeA ==
4896+
CBORObjectTypeTextStringAscii) ?
48624897
CBORObjectTypeTextString : typeA;
4863-
int tb = (typeB == CBORObjectTypeTextStringUtf8) ?
4898+
int tb = (typeB == CBORObjectTypeTextStringUtf8 || typeB ==
4899+
CBORObjectTypeTextStringAscii) ?
48644900
CBORObjectTypeTextString : typeB;
48654901
/* NOTE: itemtypeValue numbers are ordered such that they
48664902
// correspond to the lexicographical order of their CBOR encodings
@@ -5021,7 +5057,8 @@ public byte[] EncodeToBytes(CBOREncodeOptions options) {
50215057
}
50225058
if (!hasComplexTag) {
50235059
switch (this.getItemType()) {
5024-
case CBORObjectTypeTextString: {
5060+
case CBORObjectTypeTextString:
5061+
case CBORObjectTypeTextStringAscii: {
50255062
byte[] ret = GetOptimizedBytesIfShortAscii(
50265063
this.AsString(), tagged ? (((int)tagbyte) & 0xff) : -1);
50275064
if (ret != null) {
@@ -5150,13 +5187,15 @@ public boolean equals(CBORObject other) {
51505187
if (this == otherValue) {
51515188
return true;
51525189
}
5153-
if (this.itemtypeValue == CBORObjectTypeTextString &&
5190+
if ((this.itemtypeValue == CBORObjectTypeTextString ||
5191+
this.itemtypeValue == CBORObjectTypeTextStringAscii) &&
51545192
otherValue.itemtypeValue == CBORObjectTypeTextStringUtf8) {
51555193
return CBORUtilities.StringEqualsUtf8(
51565194
(String)this.itemValue,
51575195
(byte[])otherValue.itemValue);
51585196
}
5159-
if (otherValue.itemtypeValue == CBORObjectTypeTextString &&
5197+
if ((otherValue.itemtypeValue == CBORObjectTypeTextString ||
5198+
otherValue.itemtypeValue == CBORObjectTypeTextStringAscii) &&
51605199
this.itemtypeValue == CBORObjectTypeTextStringUtf8) {
51615200
return CBORUtilities.StringEqualsUtf8(
51625201
(String)otherValue.itemValue,
@@ -5234,6 +5273,7 @@ public byte[] GetByteString() {
52345273
itemHashCode = CBORArrayHashCode(this.AsList());
52355274
break;
52365275
case CBORObjectTypeTextString:
5276+
case CBORObjectTypeTextStringAscii:
52375277
itemHashCode = CBORUtilities.StringHashCode(
52385278
(String)this.itemValue);
52395279
break;
@@ -6555,7 +6595,8 @@ public void WriteTo(OutputStream stream, CBOREncodeOptions options) throws java.
65556595
stream.write(arr, 0, arr.length);
65566596
break;
65576597
}
6558-
case CBORObjectTypeTextString: {
6598+
case CBORObjectTypeTextString:
6599+
case CBORObjectTypeTextStringAscii: {
65596600
Write((String)this.getThisItem(), stream, options);
65606601
break;
65616602
}

src/main/java/com/upokecenter/cbor/CBORReader.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,12 @@ public CBORObject ReadForFirstByte(int firstbyte) throws java.io.IOException {
440440
CBORObject key = this.ReadForFirstByte(headByte);
441441
CBORObject value = this.ReadInternal();
442442
--this.depth;
443-
if (!this.options.getAllowDuplicateKeys()) {
444-
if (cbor.ContainsKey(key)) {
443+
int oldCount = cbor.size();
444+
cbor.set(key, value);
445+
int newCount = cbor.size();
446+
if (!this.options.getAllowDuplicateKeys() && oldCount == newCount) {
445447
throw new CBORException("Duplicate key already exists");
446-
}
447448
}
448-
cbor.set(key, value);
449449
}
450450
return cbor;
451451
}

0 commit comments

Comments
 (0)