Skip to content

Commit 8b88926

Browse files
committed
Use native base64 impl + add Utils aliases for all custom Type converters
1 parent b4313f2 commit 8b88926

File tree

9 files changed

+72
-208
lines changed

9 files changed

+72
-208
lines changed
Binary file not shown.

src/AndroidClient/.idea/modules/android/AndroidClient.android.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AndroidClient/.idea/modules/client/AndroidClient.client.iml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AndroidClient/android/src/main/java/net/servicestack/client/ByteArray.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ public class ByteArray {
66
public static byte[] parse(String base64) {
77
return Base64.getDecoder().decode(base64);
88
}
9+
10+
public static String toString(String source) {
11+
return toString(Utils.toUtf8Bytes(source));
12+
}
13+
914
public static String toString(byte[] bytes) {
1015
return Base64.getEncoder().encodeToString(bytes);
1116
}

src/AndroidClient/android/src/main/java/net/servicestack/client/TimeSpan.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ public static TimeSpan parse(String xsdDuration){
184184
}
185185

186186
double totalSecs = 0
187-
+ (days * 24 * 60 * 60)
188-
+ (hours * 60 * 60)
189-
+ (minutes * 60)
190-
+ (seconds);
187+
+ (days * 24 * 60 * 60)
188+
+ (hours * 60 * 60)
189+
+ (minutes * 60)
190+
+ (seconds);
191191

192192
double interval = totalSecs + ms;
193193

src/AndroidClient/android/src/main/java/net/servicestack/client/Utils.java

Lines changed: 26 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ public static ResponseStatus createResponseStatus(Object obj) {
562562
}
563563
return null;
564564
}
565+
565566
public static ResponseStatus createResponseStatus(JsonObject obj) {
566567
ResponseStatus status = new ResponseStatus();
567568
for (Map.Entry<String, JsonElement> jsonElementEntry : obj.entrySet()) {
@@ -615,6 +616,10 @@ public static ResponseStatus createResponseStatus(JsonObject obj) {
615616
return status;
616617
}
617618

619+
private static String getAsStringOrNull(JsonElement jsonElement) {
620+
return jsonElement.isJsonNull() ? null : jsonElement.getAsString();
621+
}
622+
618623
public static <T> ArrayList<T> toList(Iterable<T> iterable) {
619624
ArrayList<T> to = new ArrayList<>();
620625
for (T item : iterable) {
@@ -710,118 +715,40 @@ public static <K,V> HashMap<K,ArrayList<V>> createMap(ArrayList<V> xs, Function<
710715
return to;
711716
}
712717

713-
//From: http://iharder.sourceforge.net/current/java/base64/ (Public Domain)
714-
public static String toBase64String(String source) {
715-
return toBase64String(toUtf8Bytes(source));
718+
public static String fromDateTime(Date date) {
719+
return DateTime.toString(date);
716720
}
717721

718-
public static String toBase64String(byte[] source) {
719-
byte[] encoded = toBase64Bytes(source);
720-
try {
721-
return new String(encoded, "US-ASCII");
722-
} catch (UnsupportedEncodingException e) {
723-
return new String(encoded);
724-
}
722+
public static Date toDateTime(String jsonDate) {
723+
return DateTime.parse(jsonDate);
725724
}
726725

727-
public static byte[] toBase64Bytes(byte[] source) {
728-
return toBase64Bytes(source, 0, source.length);
726+
public static String fromTimeSpan(TimeSpan timeSpan) {
727+
return TimeSpan.toString(timeSpan);
729728
}
730729

731-
public static byte[] toBase64Bytes(byte[] source, int off, int len) {
732-
if (source == null)
733-
throw new NullPointerException("Cannot serialize a null array.");
734-
735-
if (off < 0)
736-
throw new IllegalArgumentException("Cannot have negative offset: " + off);
737-
738-
if (len < 0)
739-
throw new IllegalArgumentException("Cannot have length offset: " + len);
740-
741-
if (off + len > source.length)
742-
throw new IllegalArgumentException(String.format(
743-
"Cannot have offset of %d and length of %d with array of length %d", off, len, source.length));
744-
745-
int encLen = (len / 3) * 4 + (len % 3 > 0 ? 4 : 0); // Bytes needed for actual encoding
746-
byte[] outBuff = new byte[encLen];
747-
748-
int d = 0;
749-
int e = 0;
750-
int len2 = len - 2;
751-
for (; d < len2; d += 3, e += 4) {
752-
encode3to4(source, d + off, 3, outBuff, e);
753-
}
754-
755-
if (d < len) {
756-
encode3to4(source, d + off, len - d, outBuff, e);
757-
e += 4;
758-
}
759-
760-
// Only resize array if we didn't guess it right.
761-
if (e <= outBuff.length - 1) {
762-
byte[] finalOut = new byte[e];
763-
System.arraycopy(outBuff, 0, finalOut, 0, e);
764-
return finalOut;
765-
} else {
766-
return outBuff;
767-
}
730+
public static TimeSpan toTimeSpan(String xsdDuration) {
731+
return TimeSpan.parse(xsdDuration);
768732
}
769733

770-
private static String getAsStringOrNull(JsonElement jsonElement) {
771-
return jsonElement.isJsonNull() ? null : jsonElement.getAsString();
734+
public static String fromGuid(UUID uuid) {
735+
return Guid.toString(uuid);
772736
}
773737

774-
private final static byte EQUALS_SIGN = (byte)'=';
775-
776-
private final static byte[] _STANDARD_ALPHABET = {
777-
(byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
778-
(byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
779-
(byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
780-
(byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
781-
(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
782-
(byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
783-
(byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
784-
(byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z',
785-
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5',
786-
(byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/'
787-
};
788-
789-
private static byte[] encode3to4(
790-
byte[] source, int srcOffset, int numSigBytes,
791-
byte[] destination, int destOffset) {
738+
public static UUID toGuid(String guid) {
739+
return Guid.parse(guid);
740+
}
792741

793-
byte[] ALPHABET = _STANDARD_ALPHABET;
742+
public static byte[] toByteArray(String base64) {
743+
return ByteArray.parse(base64);
744+
}
794745

795-
int inBuff = ( numSigBytes > 0 ? ((source[ srcOffset ] << 24) >>> 8) : 0 )
796-
| ( numSigBytes > 1 ? ((source[ srcOffset + 1 ] << 24) >>> 16) : 0 )
797-
| ( numSigBytes > 2 ? ((source[ srcOffset + 2 ] << 24) >>> 24) : 0 );
746+
public static String toBase64String(String source) {
747+
return ByteArray.toString(source);
748+
}
798749

799-
switch (numSigBytes)
800-
{
801-
case 3:
802-
destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
803-
destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
804-
destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ];
805-
destination[ destOffset + 3 ] = ALPHABET[ (inBuff ) & 0x3f ];
806-
return destination;
807-
808-
case 2:
809-
destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
810-
destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
811-
destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ];
812-
destination[ destOffset + 3 ] = EQUALS_SIGN;
813-
return destination;
814-
815-
case 1:
816-
destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
817-
destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
818-
destination[ destOffset + 2 ] = EQUALS_SIGN;
819-
destination[ destOffset + 3 ] = EQUALS_SIGN;
820-
return destination;
821-
822-
default:
823-
return destination;
824-
}
750+
public static String fromByteArray(byte[] bytes) {
751+
return ByteArray.toString(bytes);
825752
}
826753

827754
public static String addQueryParam(String url, String key, String value) {

src/AndroidClient/client/src/main/java/net/servicestack/client/ByteArray.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ public class ByteArray {
66
public static byte[] parse(String base64) {
77
return Base64.getDecoder().decode(base64);
88
}
9+
10+
public static String toString(String source) {
11+
return toString(Utils.toUtf8Bytes(source));
12+
}
13+
914
public static String toString(byte[] bytes) {
1015
return Base64.getEncoder().encodeToString(bytes);
1116
}

src/AndroidClient/client/src/main/java/net/servicestack/client/TimeSpan.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,10 +184,10 @@ public static TimeSpan parse(String xsdDuration){
184184
}
185185

186186
double totalSecs = 0
187-
+ (days * 24 * 60 * 60)
188-
+ (hours * 60 * 60)
189-
+ (minutes * 60)
190-
+ (seconds);
187+
+ (days * 24 * 60 * 60)
188+
+ (hours * 60 * 60)
189+
+ (minutes * 60)
190+
+ (seconds);
191191

192192
double interval = totalSecs + ms;
193193

src/AndroidClient/client/src/main/java/net/servicestack/client/Utils.java

Lines changed: 26 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ public static ResponseStatus createResponseStatus(Object obj) {
562562
}
563563
return null;
564564
}
565+
565566
public static ResponseStatus createResponseStatus(JsonObject obj) {
566567
ResponseStatus status = new ResponseStatus();
567568
for (Map.Entry<String, JsonElement> jsonElementEntry : obj.entrySet()) {
@@ -615,6 +616,10 @@ public static ResponseStatus createResponseStatus(JsonObject obj) {
615616
return status;
616617
}
617618

619+
private static String getAsStringOrNull(JsonElement jsonElement) {
620+
return jsonElement.isJsonNull() ? null : jsonElement.getAsString();
621+
}
622+
618623
public static <T> ArrayList<T> toList(Iterable<T> iterable) {
619624
ArrayList<T> to = new ArrayList<>();
620625
for (T item : iterable) {
@@ -710,118 +715,40 @@ public static <K,V> HashMap<K,ArrayList<V>> createMap(ArrayList<V> xs, Function<
710715
return to;
711716
}
712717

713-
//From: http://iharder.sourceforge.net/current/java/base64/ (Public Domain)
714-
public static String toBase64String(String source) {
715-
return toBase64String(toUtf8Bytes(source));
718+
public static String fromDateTime(Date date) {
719+
return DateTime.toString(date);
716720
}
717721

718-
public static String toBase64String(byte[] source) {
719-
byte[] encoded = toBase64Bytes(source);
720-
try {
721-
return new String(encoded, "US-ASCII");
722-
} catch (UnsupportedEncodingException e) {
723-
return new String(encoded);
724-
}
722+
public static Date toDateTime(String jsonDate) {
723+
return DateTime.parse(jsonDate);
725724
}
726725

727-
public static byte[] toBase64Bytes(byte[] source) {
728-
return toBase64Bytes(source, 0, source.length);
726+
public static String fromTimeSpan(TimeSpan timeSpan) {
727+
return TimeSpan.toString(timeSpan);
729728
}
730729

731-
public static byte[] toBase64Bytes(byte[] source, int off, int len) {
732-
if (source == null)
733-
throw new NullPointerException("Cannot serialize a null array.");
734-
735-
if (off < 0)
736-
throw new IllegalArgumentException("Cannot have negative offset: " + off);
737-
738-
if (len < 0)
739-
throw new IllegalArgumentException("Cannot have length offset: " + len);
740-
741-
if (off + len > source.length)
742-
throw new IllegalArgumentException(String.format(
743-
"Cannot have offset of %d and length of %d with array of length %d", off, len, source.length));
744-
745-
int encLen = (len / 3) * 4 + (len % 3 > 0 ? 4 : 0); // Bytes needed for actual encoding
746-
byte[] outBuff = new byte[encLen];
747-
748-
int d = 0;
749-
int e = 0;
750-
int len2 = len - 2;
751-
for (; d < len2; d += 3, e += 4) {
752-
encode3to4(source, d + off, 3, outBuff, e);
753-
}
754-
755-
if (d < len) {
756-
encode3to4(source, d + off, len - d, outBuff, e);
757-
e += 4;
758-
}
759-
760-
// Only resize array if we didn't guess it right.
761-
if (e <= outBuff.length - 1) {
762-
byte[] finalOut = new byte[e];
763-
System.arraycopy(outBuff, 0, finalOut, 0, e);
764-
return finalOut;
765-
} else {
766-
return outBuff;
767-
}
730+
public static TimeSpan toTimeSpan(String xsdDuration) {
731+
return TimeSpan.parse(xsdDuration);
768732
}
769733

770-
private static String getAsStringOrNull(JsonElement jsonElement) {
771-
return jsonElement.isJsonNull() ? null : jsonElement.getAsString();
734+
public static String fromGuid(UUID uuid) {
735+
return Guid.toString(uuid);
772736
}
773737

774-
private final static byte EQUALS_SIGN = (byte)'=';
775-
776-
private final static byte[] _STANDARD_ALPHABET = {
777-
(byte)'A', (byte)'B', (byte)'C', (byte)'D', (byte)'E', (byte)'F', (byte)'G',
778-
(byte)'H', (byte)'I', (byte)'J', (byte)'K', (byte)'L', (byte)'M', (byte)'N',
779-
(byte)'O', (byte)'P', (byte)'Q', (byte)'R', (byte)'S', (byte)'T', (byte)'U',
780-
(byte)'V', (byte)'W', (byte)'X', (byte)'Y', (byte)'Z',
781-
(byte)'a', (byte)'b', (byte)'c', (byte)'d', (byte)'e', (byte)'f', (byte)'g',
782-
(byte)'h', (byte)'i', (byte)'j', (byte)'k', (byte)'l', (byte)'m', (byte)'n',
783-
(byte)'o', (byte)'p', (byte)'q', (byte)'r', (byte)'s', (byte)'t', (byte)'u',
784-
(byte)'v', (byte)'w', (byte)'x', (byte)'y', (byte)'z',
785-
(byte)'0', (byte)'1', (byte)'2', (byte)'3', (byte)'4', (byte)'5',
786-
(byte)'6', (byte)'7', (byte)'8', (byte)'9', (byte)'+', (byte)'/'
787-
};
788-
789-
private static byte[] encode3to4(
790-
byte[] source, int srcOffset, int numSigBytes,
791-
byte[] destination, int destOffset) {
738+
public static UUID toGuid(String guid) {
739+
return Guid.parse(guid);
740+
}
792741

793-
byte[] ALPHABET = _STANDARD_ALPHABET;
742+
public static byte[] toByteArray(String base64) {
743+
return ByteArray.parse(base64);
744+
}
794745

795-
int inBuff = ( numSigBytes > 0 ? ((source[ srcOffset ] << 24) >>> 8) : 0 )
796-
| ( numSigBytes > 1 ? ((source[ srcOffset + 1 ] << 24) >>> 16) : 0 )
797-
| ( numSigBytes > 2 ? ((source[ srcOffset + 2 ] << 24) >>> 24) : 0 );
746+
public static String toBase64String(String source) {
747+
return ByteArray.toString(source);
748+
}
798749

799-
switch (numSigBytes)
800-
{
801-
case 3:
802-
destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
803-
destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
804-
destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ];
805-
destination[ destOffset + 3 ] = ALPHABET[ (inBuff ) & 0x3f ];
806-
return destination;
807-
808-
case 2:
809-
destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
810-
destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
811-
destination[ destOffset + 2 ] = ALPHABET[ (inBuff >>> 6) & 0x3f ];
812-
destination[ destOffset + 3 ] = EQUALS_SIGN;
813-
return destination;
814-
815-
case 1:
816-
destination[ destOffset ] = ALPHABET[ (inBuff >>> 18) ];
817-
destination[ destOffset + 1 ] = ALPHABET[ (inBuff >>> 12) & 0x3f ];
818-
destination[ destOffset + 2 ] = EQUALS_SIGN;
819-
destination[ destOffset + 3 ] = EQUALS_SIGN;
820-
return destination;
821-
822-
default:
823-
return destination;
824-
}
750+
public static String fromByteArray(byte[] bytes) {
751+
return ByteArray.toString(bytes);
825752
}
826753

827754
public static String addQueryParam(String url, String key, String value) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy