Java Conversion from byte[] to char[] - java

I have wrote a simple snippet in which I try to convert (maybe) a byte array in char array e vice versa.
There are a lot of examples on the Net but this is for me the simplest way to do it.
I need to use array and not strings because its content is a password field to manage.
So I ask you, is this snippet correct?
private static char[] fromByteToCharArrayConverter(byte[] byteArray){
ByteBuffer buffer = ByteBuffer.wrap(byteArray);
clearArray(byteArray);
CharBuffer charBuffer = Charset.forName("UTF-8").decode(buffer);
char[] charArray = new char[charBuffer.remaining()];
charBuffer.get(charArray);
return charArray;
}
private static byte[] fromCharToByteArray(char[] charArray){
CharBuffer charBuffer = CharBuffer.wrap(charArray);
ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
byte[] byteArray = new byte[byteBuffer.remaining()];
byteBuffer.get(byteArray);
return byteArray;
}
Thanks

No, that won't work for (at least) the following reason:
ByteBuffer buffer = ByteBuffer.wrap(byteArray); // Wrap the array
clearArray(byteArray); // Clear the array -> ByteBuffer cleared too

Related

Cast string array elements to byte array in java [duplicate]

This question already has answers here:
How to convert a String array to a Byte array? (java)
(7 answers)
Closed 7 years ago.
I have a string array of form:
String[] s = {0x22, 0xD2, 0x01}
Now I have to convert it to byte array form like:
byte[] bytes = {(byte)0x22, (byte)0xD2, (byte)0x01}
It can be done in single line in c# but how to do it in Java as I have to append bytes array to another array of same kind and format.
Here I have included some part of the code as I can't include whole code:
String sr = "22D201";
String[] s = {sr.substring(0, 2),sr.substring(2, 4),sr.substring(4)};
byte[] ret = new byte[]{(byte)0x2C, (byte)0x04, (byte)0x01, (byte)0x67, (byte)0x00, (byte)0x00, (byte)0x3D};
Now I have to append byte[] bytes to byte[] ret but I can't as array is in the form of String that is String[] s. So how to covert String[] s so that I can add it to byte[] ret.
You can use String.getBytes();.
You can also initialize a String using a byte array and a specified encoding scheme:
String s = new String(new byte[]{ /* Bytes data. */}, "UTF-8");
For an array of Strings, each individual String's byte array could therefore be processed as follows:
for(final String s : lStrings) {
byte[] lBytes = s.getBytes();
}
If you wanted to make a contiguous array of these types, you could use a ByteArrayOutputStream.
ByteArrayOutputStream b = new ByteArrayOutputStream();
for(final String s : lStrings) {
b.write(s.getBytes());
}
final byte[] lTotalBytes = b.toByteArray();
/* Make sure all the bytes are written. */
b.flush();
/* Close the stream once we're finished. */
b.close();

char and byte buffer encoding & decoding

I'm trying to understand the encoding way, here is my code to encode and decode a string.
Charset utfset = Charset.forName("UTF-8");
CharsetEncoder encoder = utfset.newEncoder();
String text = "java.abcded.tocken";
CharBuffer cb = CharBuffer.wrap(text.toCharArray());
ByteBuffer bb = encoder.encode(cb);
byte[] bytes = bb.array();
CharsetDecoder isodecoder = utfset.newDecoder();
CharBuffer isodcb = isodecoder.decode(bb);
System.out.println(String.valueOf(cb.array()).equals(String.valueOf(isodcb.array())));
CharBuffer isodcb2 = isodecoder.decode(ByteBuffer.wrap(bytes));
System.out.println(String.valueOf(cb.array()).equals(String.valueOf(isodcb2.array())));
When the decode is performed with byteBuffer itself, the strings are equal but, when the decode is performed with bytebuffer.wrap of the byte array from bytebuffer, the strings are not equal. It is appending spaces to the end, is there a reason behind it ?
CharsetEncoder.encode makes no guarantees about the underlying array size, nor that the ByteBuffer will actually be backed by an array. The array backing the buffer is larger than the number of bytes contained in it.
You should see different numbers if you run this code:
CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder();
String text = "java.abcded.tocken";
CharBuffer cb = CharBuffer.wrap(text.toCharArray());
ByteBuffer bb = encoder.encode(cb);
System.out.println(bb.remaining());
System.out.println(bb.array().length);

Decoding multibyte UTF8 symbols with charset decoder in byte-by-byte manner?

I am trying to decode UTF8 byte by byte with charset decoder. Is this possible?
The following code
public static void main(String[] args) {
Charset cs = Charset.forName("utf8");
CharsetDecoder decoder = cs.newDecoder();
CoderResult res;
byte[] source = new byte[] {(byte)0xc3, (byte)0xa6}; // LATIN SMALL LETTER AE in UTF8
byte[] b = new byte[1];
ByteBuffer bb = ByteBuffer.wrap(b);
char[] c = new char[1];
CharBuffer cb = CharBuffer.wrap(c);
decoder.reset();
b[0] = source[0];
bb.rewind();
cb.rewind();
res = decoder.decode(bb, cb, false);
System.out.println(res);
System.out.println(cb.remaining());
b[0] = source[1];
bb.rewind();
cb.rewind();
res = decoder.decode(bb, cb, false);
System.out.println(res);
System.out.println(cb.remaining());
}
gives the following output.
UNDERFLOW
1
MALFORMED[1]
1
Why?
My theory is that the problem with the way that you are doing it is that in the "underflow" condition, the decoder leaves the unconsumed bytes in the input buffer. At least, that is my reading.
Note this sentence in the javadoc:
"In any case, if this method is to be reinvoked in the same decoding operation then care should be taken to preserve any bytes remaining in the input buffer so that they are available to the next invocation. "
But you are clobbering the (presumably) unread byte.
You should be able to check whether my theory / interpretation is correct by looking at how many bytes remain unconsumed in bb after the first decode(...) call.
If my theory is correct then the answer is that you cannot decode UTF-8 by providing the decoder with byte buffers containing exactly one byte. But you could implement byte-by-byte decoding by starting with a ByteBuffer containing one byte and adding extra bytes until the decoder succeeds in outputing a character. Just make sure that you don't clobber input bytes that haven't been consumed yet.
Note that decoding like this is not efficient. The API design is optimized for decoding a large number of bytes in one go.
As has been said, utf has 1-6 bytes per char. you need to add all bytes to the bytebuffer before you decode try this:
public static void main(String[] args) {
Charset cs = Charset.forName("utf8");
CharsetDecoder decoder = cs.newDecoder();
CoderResult res;
byte[] source = new byte[] {(byte)0xc3, (byte)0xa6}; // LATIN SMALL LETTER AE in UTF8
byte[] b = new byte[2]; //two bytes for this char
ByteBuffer bb = ByteBuffer.wrap(b);
char[] c = new char[1];
CharBuffer cb = CharBuffer.wrap(c);
decoder.reset();
b[0] = source[0];
b[1] = source[1];
bb.rewind();
cb.rewind();
res = decoder.decode(bb, cb, false); //translates 2 bytes to 1 char
System.out.println(cb.remaining()); //prints 0
System.out.println(cb.get(0)); //prints latin ae
}

How to convert two longs to a byte array = how to convert UUID to byte array?

I am using Javas UUID and need to convert a UUID to a byte Array. Strangely the UUID Class does not provide a "toBytes()" method.
I already found out about the two methods:
UUID.getMostSignificantBits()
and
UUID.getLeasSignificantBits()
But how to get this into a byte array? the result should be a byte[] with those tow values. I somehow need to do Bitshifting but, how?
update:
I found:
ByteBuffer byteBuffer = MappedByteBuffer.allocate(2);
byteBuffer.putLong(uuid.getMostSignificantBits());
byteBuffer.putLong(uuid.getLeastSignificantBits());
Is this approach corret?
Are there any other methods (for learning purposes)?
Thanks very much!!
Jens
You can use ByteBuffer
byte[] bytes = new byte[16];
ByteBuffer bb = ByteBuffer.wrap(bytes);
bb.order(ByteOrder.LITTLE_ENDIAN or ByteOrder.BIG_ENDIAN);
bb.putLong(UUID.getMostSignificantBits());
bb.putLong(UUID.getLeastSignificantBits());
// to reverse
bb.flip();
UUID uuid = new UUID(bb.getLong(), bb.getLong());
One option if you prefer "regular" IO to NIO:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.write(uuid.getMostSignificantBits());
dos.write(uuid.getLeastSignificantBits());
dos.flush(); // May not be necessary
byte[] data = dos.toByteArray();
For anyone trying to use this in Java 1.7, I found the following to be necessary:
<!-- language: lang-java -->
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(password.getMostSignificantBits());
dos.writeLong(password.getLeastSignificantBits());
dos.flush(); // May not be necessary
return baos.toByteArray();

Java - Byte[] to byte[]

There is Vector and DataOutputStream. I need to write bytes from Vector (toArray returns Byte[]) to the stream, but it understands byte[] only. How to convert Byte[] to byte[] ?
You could use the toPrimitive method in the Apache Commons lang library ArrayUtils class?
byte[] toPrimitives(Byte[] oBytes)
{
byte[] bytes = new byte[oBytes.length];
for(int i = 0; i < oBytes.length; i++) {
bytes[i] = oBytes[i];
}
return bytes;
}
Inverse:
// byte[] to Byte[]
Byte[] toObjects(byte[] bytesPrim) {
Byte[] bytes = new Byte[bytesPrim.length];
int i = 0;
for (byte b : bytesPrim) bytes[i++] = b; // Autoboxing
return bytes;
}
freeone3000 contributed in this answer :)
A Vector<Byte> is about as inefficient structure as you could use to store bytes. I would serious consider using something more efficient line ByteArrayOutputStream which has a toByteArray() method. i.e. don't just convert the Vector but remove it from the code.

Categories