forked from jdereg/java-util
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request jdereg#87 from kpartlow/master
Added StringBuffer, StringBuidler, ByteBuffer, CharBuffer, byte[], ch…
- Loading branch information
Showing
9 changed files
with
501 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/cedarsoftware/util/convert/ByteArrayConversions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.cedarsoftware.util.convert; | ||
|
||
import com.cedarsoftware.util.StringUtilities; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.CharBuffer; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
public class ByteArrayConversions { | ||
|
||
static String toString(Object from, ConverterOptions options) { | ||
byte[] bytes = (byte[])from; | ||
return (bytes == null) ? StringUtilities.EMPTY : new String(bytes, options.getCharset()); | ||
} | ||
|
||
static ByteBuffer toByteBuffer(Object from) { | ||
return ByteBuffer.wrap((byte[])from); | ||
} | ||
|
||
static String toString(Object from, Converter converter, ConverterOptions options) { | ||
return toString(from, options); | ||
} | ||
|
||
static ByteBuffer toByteBuffer(Object from, Converter converter, ConverterOptions options) { | ||
return toByteBuffer(from); | ||
} | ||
|
||
static CharBuffer toCharBuffer(Object from, Converter converter, ConverterOptions options) { | ||
return CharBuffer.wrap(toString(from, options)); | ||
} | ||
|
||
static char[] toCharArray(Object from, Converter converter, ConverterOptions options) { | ||
return toString(from, options).toCharArray(); | ||
} | ||
|
||
static StringBuffer toStringBuffer(Object from, Converter converter, ConverterOptions options) { | ||
return new StringBuffer(toString(from, options)); | ||
} | ||
|
||
static StringBuilder toStringBuilder(Object from, Converter converter, ConverterOptions options) { | ||
return new StringBuilder(toString(from, options)); | ||
} | ||
|
||
} |
66 changes: 66 additions & 0 deletions
66
src/main/java/com/cedarsoftware/util/convert/ByteBufferConversions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.cedarsoftware.util.convert; | ||
|
||
import com.cedarsoftware.util.StringUtilities; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.CharBuffer; | ||
|
||
import static com.cedarsoftware.util.ArrayUtilities.EMPTY_BYTE_ARRAY; | ||
|
||
public class ByteBufferConversions { | ||
|
||
static ByteBuffer asReadOnlyBuffer(Object from) { | ||
// Create a readonly buffer so we aren't changing | ||
// the original buffers mark and position when | ||
// working with this buffer. This could be inefficient | ||
// if constantly fed with writeable buffers so should be documented | ||
return ((ByteBuffer)from).asReadOnlyBuffer(); | ||
} | ||
|
||
static byte[] toByteArray(Object from) { | ||
ByteBuffer buffer = asReadOnlyBuffer(from); | ||
|
||
if (buffer == null || !buffer.hasRemaining()) { | ||
return EMPTY_BYTE_ARRAY; | ||
} | ||
|
||
byte[] bytes = new byte[buffer.remaining()]; | ||
buffer.get(bytes); | ||
return bytes; | ||
} | ||
|
||
static CharBuffer toCharBuffer(Object from, ConverterOptions options) { | ||
ByteBuffer buffer = asReadOnlyBuffer(from); | ||
return options.getCharset().decode(buffer); | ||
} | ||
|
||
|
||
static CharBuffer toCharBuffer(Object from, Converter converter, ConverterOptions options) { | ||
return toCharBuffer(from, options); | ||
} | ||
|
||
static ByteBuffer toByteBuffer(Object from, Converter converter, ConverterOptions options) { | ||
return asReadOnlyBuffer(from); | ||
} | ||
|
||
static byte[] toByteArray(Object from, Converter converter, ConverterOptions options) { | ||
return toByteArray(from); | ||
} | ||
|
||
static String toString(Object from, Converter converter, ConverterOptions options) { | ||
return toCharBuffer(from, options).toString(); | ||
} | ||
|
||
static char[] toCharArray(Object from, Converter converter, ConverterOptions options) { | ||
return CharBufferConversions.toCharArray(toCharBuffer(from, options)); | ||
} | ||
|
||
static StringBuffer toStringBuffer(Object from, Converter converter, ConverterOptions options) { | ||
return new StringBuffer(toCharBuffer(from, options)); | ||
} | ||
|
||
static StringBuilder toStringBuilder(Object from, Converter converter, ConverterOptions options) { | ||
return new StringBuilder(toCharBuffer(from, options)); | ||
} | ||
|
||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/com/cedarsoftware/util/convert/CharArrayConversions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.cedarsoftware.util.convert; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.CharBuffer; | ||
import java.util.Arrays; | ||
|
||
public class CharArrayConversions { | ||
|
||
static String toString(Object from) { | ||
char[] chars = (char[])from; | ||
return new String(chars); | ||
} | ||
|
||
static CharBuffer toCharBuffer(Object from) { | ||
char[] chars = (char[])from; | ||
return CharBuffer.wrap(chars); | ||
} | ||
|
||
static ByteBuffer toByteBuffer(Object from, ConverterOptions options) { | ||
return options.getCharset().encode(toCharBuffer(from)); | ||
} | ||
|
||
|
||
static String toString(Object from, Converter converter, ConverterOptions options) { | ||
return toString(from); | ||
} | ||
|
||
|
||
static CharBuffer toCharBuffer(Object from, Converter converter, ConverterOptions options) { | ||
return toCharBuffer(from); | ||
} | ||
|
||
static StringBuffer toStringBuffer(Object from, Converter converter, ConverterOptions options) { | ||
return new StringBuffer(toCharBuffer(from)); | ||
} | ||
|
||
static StringBuilder toStringBuilder(Object from, Converter converter, ConverterOptions options) { | ||
return new StringBuilder(toCharBuffer(from)); | ||
} | ||
|
||
static ByteBuffer toByteBuffer(Object from, Converter converter, ConverterOptions options) { | ||
return toByteBuffer(from, options); | ||
} | ||
|
||
static byte[] toByteArray(Object from, Converter converter, ConverterOptions options) { | ||
ByteBuffer buffer = toByteBuffer(from, options); | ||
byte[] byteArray = new byte[buffer.remaining()]; | ||
buffer.get(byteArray); | ||
return byteArray; | ||
} | ||
|
||
static char[] toCharArray(Object from, Converter converter, ConverterOptions options) { | ||
char[] chars = (char[])from; | ||
if (chars == null) { | ||
return null; | ||
} | ||
return Arrays.copyOf(chars, chars.length); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
src/main/java/com/cedarsoftware/util/convert/CharBufferConversions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.cedarsoftware.util.convert; | ||
|
||
import java.nio.ByteBuffer; | ||
import java.nio.CharBuffer; | ||
|
||
import static com.cedarsoftware.util.ArrayUtilities.EMPTY_BYTE_ARRAY; | ||
import static com.cedarsoftware.util.ArrayUtilities.EMPTY_CHAR_ARRAY; | ||
|
||
public class CharBufferConversions { | ||
static CharBuffer asReadOnlyBuffer(Object from) { | ||
// Create a readonly buffer so we aren't changing | ||
// the original buffers mark and position when | ||
// working with this buffer. This could be inefficient | ||
// if constantly fed with writeable buffers so should be documented | ||
return ((CharBuffer)from).asReadOnlyBuffer(); | ||
} | ||
|
||
static char[] toCharArray(Object from) { | ||
CharBuffer buffer = asReadOnlyBuffer(from); | ||
|
||
if (buffer == null || !buffer.hasRemaining()) { | ||
return EMPTY_CHAR_ARRAY; | ||
} | ||
|
||
char[] chars = new char[buffer.remaining()]; | ||
buffer.get(chars); | ||
return chars; | ||
} | ||
|
||
static CharBuffer toCharBuffer(Object from, Converter converter, ConverterOptions options) { | ||
return asReadOnlyBuffer(from); | ||
} | ||
|
||
static byte[] toByteArray(Object from, Converter converter, ConverterOptions options) { | ||
return ByteBufferConversions.toByteArray(toByteBuffer(from, converter, options)); | ||
} | ||
|
||
static ByteBuffer toByteBuffer(Object from, Converter converter, ConverterOptions options) { | ||
return options.getCharset().encode(asReadOnlyBuffer(from)); | ||
} | ||
|
||
static String toString(Object from, Converter converter, ConverterOptions options) { | ||
return asReadOnlyBuffer(from).toString(); | ||
} | ||
|
||
static char[] toCharArray(Object from, Converter converter, ConverterOptions options) { | ||
return toCharArray(from); | ||
} | ||
|
||
static StringBuffer toStringBuffer(Object from, Converter converter, ConverterOptions options) { | ||
return new StringBuffer(asReadOnlyBuffer(from)); | ||
} | ||
|
||
static StringBuilder toStringBuilder(Object from, Converter converter, ConverterOptions options) { | ||
return new StringBuilder(asReadOnlyBuffer(from)); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/cedarsoftware/util/convert/ClassConversions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.cedarsoftware.util.convert; | ||
|
||
public class ClassConversions { | ||
static String toString(Object from, Converter converter, ConverterOptions options) { | ||
Class<?> cls = (Class<?>) from; | ||
return cls.getName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.