Skip to content

Commit 24f75fe

Browse files
committed
Version 4.4.2
1 parent f01a995 commit 24f75fe

File tree

8 files changed

+441
-309
lines changed

8 files changed

+441
-309
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ project, add the following to the `dependencies` section in your `pom.xml` file:
2424
<dependency>
2525
<groupId>com.upokecenter</groupId>
2626
<artifactId>cbor</artifactId>
27-
<version>4.4.1</version>
27+
<version>4.4.2</version>
2828
</dependency>
2929
```
3030

pom.xml

+1-1
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-SNAPSHOT</version>
7+
<version>4.4.2</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/CBORDateConverter.java

+29-13
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ public boolean TryGetDateTimeFields(CBORObject obj, EInteger[] year, int[]
201201
lesserFields) {
202202
// TODO: In next major version, return false instead of throwing an
203203
// exception if the arguments are invalid, to conform to convention
204-
// with Try* methods in DotNet
204+
// with Try* methods in DotNet.
205+
// TODO: In next minor version, add overload that takes an out parameter
206+
// for year to DotNet version.
205207
if (year == null) {
206208
throw new NullPointerException("year");
207209
}
@@ -272,12 +274,19 @@ private String TryGetDateTimeFieldsInternal(
272274
num.compareTo(Long.MAX_VALUE) > 0) {
273275
return "Too big or small to fit a java.util.Date";
274276
}
275-
EDecimal dec;
276-
dec = (EDecimal)untagobj.ToObject(EDecimal.class);
277-
CBORUtilities.BreakDownSecondsSinceEpoch(
278-
dec,
279-
outYear,
280-
lesserFields);
277+
if (num.CanFitInInt64()) {
278+
CBORUtilities.BreakDownSecondsSinceEpoch(
279+
num.ToInt64Checked(),
280+
outYear,
281+
lesserFields);
282+
} else {
283+
EDecimal dec;
284+
dec = (EDecimal)untagobj.ToObject(EDecimal.class);
285+
CBORUtilities.BreakDownSecondsSinceEpoch(
286+
dec,
287+
outYear,
288+
lesserFields);
289+
}
281290
return null; // no error
282291
}
283292
if (obj.HasMostOuterTag(0)) {
@@ -301,12 +310,19 @@ private String TryGetDateTimeFieldsInternal(
301310
if (!num.IsFinite()) {
302311
return "Not a finite number";
303312
}
304-
EDecimal dec;
305-
dec = (EDecimal)untagobj.ToObject(EDecimal.class);
306-
CBORUtilities.BreakDownSecondsSinceEpoch(
307-
dec,
308-
outYear,
309-
lesserFields);
313+
if (num.CanFitInInt64()) {
314+
CBORUtilities.BreakDownSecondsSinceEpoch(
315+
num.ToInt64Checked(),
316+
outYear,
317+
lesserFields);
318+
} else {
319+
EDecimal dec;
320+
dec = (EDecimal)untagobj.ToObject(EDecimal.class);
321+
CBORUtilities.BreakDownSecondsSinceEpoch(
322+
dec,
323+
outYear,
324+
lesserFields);
325+
}
310326
return null; // No error
311327
}
312328
return "Not tag 0 or 1";

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

-6
Original file line numberDiff line numberDiff line change
@@ -969,12 +969,6 @@ String ToJSONString() {
969969
}
970970
case ERational: {
971971
ERational dec = (ERational)this.value;
972-
String nnstr = dec.getNumerator().toString();
973-
String dnstr = dec.getDenominator().toString();
974-
// System.out.println(
975-
// "numlen="+nnstr.length() +
976-
// " denlen="+dnstr.length() +
977-
// "\nstart="+java.util.Date.UtcNow);
978972
EDecimal f = dec.ToEDecimalExactIfPossible(
979973
EContext.Decimal128.WithUnlimitedExponents());
980974
// System.out.println(

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ private static EInteger FloorDiv(EInteger a, EInteger n) {
618618
}
619619

620620
private static long FloorDiv(long longA, int longN) {
621-
return longA >= 0 ? longA / longN : (-1 - longA) / longN;
621+
return longA >= 0 ? longA / longN : (-1 - ((-1 - longA) / longN));
622622
}
623623

624624
private static EInteger FloorMod(EInteger a, EInteger n) {
@@ -627,7 +627,7 @@ private static EInteger FloorMod(EInteger a, EInteger n) {
627627

628628
private static long FloorModLong(long longA, int longN) {
629629
{
630-
return longA - FloorDiv(longA, longN) * longN;
630+
return longA - (FloorDiv(longA, longN) * longN);
631631
}
632632
}
633633

@@ -897,7 +897,9 @@ public static void BreakDownSecondsSinceEpoch(
897897
EInteger[] normPart = new EInteger[3];
898898
long longDays = FloorDiv(seconds, 86400) + 1;
899899
long longSecondsInDay = FloorModLong(seconds, 86400);
900+
900901
int secondsInDay = ((int)longSecondsInDay);
902+
901903
GetNormalizedPartProlepticGregorian(
902904
EInteger1970,
903905
1,
@@ -917,8 +919,8 @@ public static void BreakDownSecondsSinceEpoch(
917919
int[] lesserFields) {
918920
EInteger integerPart = edec.Quantize(0, ERounding.Floor)
919921
.ToEInteger();
920-
EDecimal fractionalPart = edec.Abs()
921-
.Subtract(EDecimal.FromEInteger(integerPart).Abs());
922+
EDecimal fractionalPart = edec.Subtract(
923+
EDecimal.FromEInteger(integerPart)).Abs();
922924
int fractionalSeconds = fractionalPart.Multiply(FractionalSeconds)
923925
.ToInt32Checked();
924926
EInteger days = FloorDiv(
@@ -927,6 +929,7 @@ public static void BreakDownSecondsSinceEpoch(
927929
int secondsInDay = FloorMod(
928930
integerPart,
929931
EInteger86400).ToInt32Checked();
932+
930933
GetNormalizedPartProlepticGregorian(
931934
EInteger1970,
932935
1,

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

+42-3
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,35 @@ public static CBORObject FromArray(final Object arr, PODOptions options,
330330
CBORTypeMapper mapper, int depth) {
331331
int length = Array.getLength(arr);
332332
CBORObject obj = CBORObject.NewArray();
333+
if(arr instanceof int[]) {
334+
int[] iarr=(int[])arr;
335+
if(mapper==null && options==null) {
336+
for(int i = 0;i < length;i++) {
337+
obj.Add(CBORObject.FromObject((int)iarr[i]));
338+
}
339+
} else {
340+
for(int i = 0;i < length;i++) {
341+
obj.Add(CBORObject.FromObject((int)iarr[i], options,
342+
mapper, depth+1));
343+
}
344+
}
345+
return obj;
346+
}
347+
if(arr instanceof Integer[]) {
348+
Integer[] iarr=(Integer[])arr;
349+
if(mapper==null && options==null) {
350+
for(int i = 0;i < length;i++) {
351+
obj.Add(CBORObject.FromObject((int)iarr[i]));
352+
}
353+
} else {
354+
for(int i = 0;i < length;i++) {
355+
obj.Add(CBORObject.FromObject((int)iarr[i], options,
356+
mapper, depth+1));
357+
}
358+
}
359+
return obj;
360+
}
361+
System.out.println("Array length "+length);
333362
for(int i = 0;i < length;i++) {
334363
obj.Add(CBORObject.FromObject(Array.get(arr,i), options,
335364
mapper, depth+1));
@@ -880,14 +909,24 @@ public static Object CallFromObject(
880909
// Fractional seconds divided by milliseconds
881910
private static final int TicksDivFracSeconds = CBORUtilities.FractionalSeconds/1000;
882911

912+
private static long FloorDiv(long longA, int longN) {
913+
return longA >= 0 ? longA / longN : (-1 - ((-1 - longA) / longN));
914+
}
915+
916+
private static long FloorModLong(long longA, int longN) {
917+
return longA - (FloorDiv(longA, longN) * longN);
918+
}
919+
883920
public static void BreakDownDateTime(java.util.Date bi,
884921
EInteger[] year, int[] lf) {
885922
if(TicksDivFracSeconds == 0)throw new IllegalStateException();
886923
long time=bi.getTime();
887-
int nanoseconds=((int)(time%1000L));
888-
if(nanoseconds<0)nanoseconds=1000+nanoseconds;
924+
int nanoseconds=(int)FloorModLong(time,1000);
925+
if(time<0 && nanoseconds!=0){
926+
// nanoseconds=1000-nanoseconds;
927+
}
889928
nanoseconds*=TicksDivFracSeconds;
890-
long seconds=time/1000L;
929+
long seconds=FloorDiv(time,1000);
891930
CBORUtilities.BreakDownSecondsSinceEpoch(seconds,year,lf);
892931
lf[5]=nanoseconds;
893932
}

src/test/java/com/upokecenter/test/CBORObjectTest.java

+61
Original file line numberDiff line numberDiff line change
@@ -8976,6 +8976,67 @@ private static String DateTimeToString(
89768976
return new String(charbuf);
89778977
}
89788978

8979+
private static void TestDateTimeStringNumberOne(String str, long num) {
8980+
CBORObject dtstring = CBORObject.FromObject(str).WithTag(0);
8981+
CBORObject dtnum = CBORObject.FromObject(num).WithTag(1);
8982+
TestDateTimeStringNumberOne(dtstring, dtnum);
8983+
}
8984+
private static void TestDateTimeStringNumberOne(String str, double num) {
8985+
CBORObject dtstring = CBORObject.FromObject(str).WithTag(0);
8986+
CBORObject dtnum = CBORObject.FromObject(num).WithTag(1);
8987+
TestDateTimeStringNumberOne(dtstring, dtnum);
8988+
}
8989+
private static void TestDateTimeStringNumberOne(CBORObject dtstring,
8990+
CBORObject dtnum) {
8991+
CBORDateConverter convNumber = CBORDateConverter.TaggedNumber;
8992+
CBORDateConverter convString = CBORDateConverter.TaggedString;
8993+
CBORObject cbor;
8994+
EInteger[] eiYear = new EInteger[1];
8995+
int[] lesserFields = new int[7];
8996+
String strnum = dtstring + ", " + dtnum;
8997+
cbor = convNumber.ToCBORObject(convNumber.FromCBORObject(dtstring));
8998+
Assert.assertEquals(strnum, dtnum, cbor);
8999+
if (!convNumber.TryGetDateTimeFields(dtstring, eiYear, lesserFields)) {
9000+
Assert.fail(strnum);
9001+
}
9002+
cbor = convNumber.DateTimeFieldsToCBORObject(eiYear[0], lesserFields);
9003+
Assert.assertEquals(strnum, dtnum, cbor);
9004+
cbor = convString.DateTimeFieldsToCBORObject(eiYear[0], lesserFields);
9005+
Assert.assertEquals(strnum, dtstring, cbor);
9006+
cbor = convString.ToCBORObject(convString.FromCBORObject(dtnum));
9007+
Assert.assertEquals(strnum, dtstring, cbor);
9008+
if (!convString.TryGetDateTimeFields(dtnum, eiYear, lesserFields)) {
9009+
Assert.fail(strnum);
9010+
}
9011+
cbor = convNumber.DateTimeFieldsToCBORObject(eiYear[0], lesserFields);
9012+
Assert.assertEquals(strnum, dtnum, cbor);
9013+
cbor = convString.DateTimeFieldsToCBORObject(eiYear[0], lesserFields);
9014+
Assert.assertEquals(strnum, dtstring, cbor);
9015+
}
9016+
9017+
@Test
9018+
public void TestDateTimeStringNumber() {
9019+
TestDateTimeStringNumberOne("1970-01-01T00:00:00.25Z", 0.25);
9020+
TestDateTimeStringNumberOne("1970-01-01T00:00:00.75Z", 0.75);
9021+
TestDateTimeStringNumberOne("1969-12-31T23:59:59.75Z", -0.25);
9022+
TestDateTimeStringNumberOne("1969-12-31T23:59:59.25Z", -0.75);
9023+
TestDateTimeStringNumberOne("1970-01-03T00:00:00Z", 172800);
9024+
TestDateTimeStringNumberOne("1970-01-03T00:00:00Z", 172800);
9025+
TestDateTimeStringNumberOne("1970-01-03T00:00:00Z", 172800);
9026+
TestDateTimeStringNumberOne("2001-01-03T00:00:00Z", 978480000);
9027+
TestDateTimeStringNumberOne("2001-01-03T00:00:00.25Z", 978480000.25);
9028+
TestDateTimeStringNumberOne("1960-01-03T00:00:00Z", -315446400);
9029+
TestDateTimeStringNumberOne("1400-01-03T00:00:00Z", -17987270400L);
9030+
TestDateTimeStringNumberOne("2100-01-03T00:00:00Z", 4102617600L);
9031+
TestDateTimeStringNumberOne("1970-01-03T00:00:01Z", 172801);
9032+
TestDateTimeStringNumberOne("2001-01-03T00:00:01Z", 978480001);
9033+
TestDateTimeStringNumberOne("1960-01-03T00:00:01Z", -315446399);
9034+
TestDateTimeStringNumberOne("1960-01-03T00:00:00.25Z", -315446399.75);
9035+
TestDateTimeStringNumberOne("1960-01-03T00:00:00.75Z", -315446399.25);
9036+
TestDateTimeStringNumberOne("1400-01-03T00:00:01Z", -17987270399L);
9037+
TestDateTimeStringNumberOne("2100-01-03T00:00:01Z", 4102617601L);
9038+
}
9039+
89799040
@Test
89809041
public void TestDateTime() {
89819042
ArrayList<String> dateList = new ArrayList<String>();

0 commit comments

Comments
 (0)