Adds string to byte array converter

This commit is contained in:
konsoletyper 2013-12-20 16:31:18 +04:00
parent 55218aba4b
commit 971ad3c95e
3 changed files with 97 additions and 6 deletions

View File

@ -15,12 +15,10 @@
*/ */
package org.teavm.classlib.java.lang; package org.teavm.classlib.java.lang;
import org.teavm.classlib.impl.charset.ByteBuffer; import org.teavm.classlib.impl.charset.*;
import org.teavm.classlib.impl.charset.CharBuffer;
import org.teavm.classlib.impl.charset.Charset;
import org.teavm.classlib.impl.charset.UTF16Helper;
import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.io.TSerializable;
import org.teavm.classlib.java.io.TUnsupportedEncodingException; import org.teavm.classlib.java.io.TUnsupportedEncodingException;
import org.teavm.classlib.java.util.TArrays;
import org.teavm.javascript.ni.GeneratedBy; import org.teavm.javascript.ni.GeneratedBy;
import org.teavm.javascript.ni.Rename; import org.teavm.javascript.ni.Rename;
@ -63,7 +61,7 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
} }
public TString(byte[] bytes, int offset, int length) { public TString(byte[] bytes, int offset, int length) {
initWithBytes(bytes, offset, length, Charset.get("UTF-8")); initWithBytes(bytes, offset, length, new UTF8Charset());
} }
public TString(byte[] bytes) { public TString(byte[] bytes) {
@ -487,6 +485,37 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
return true; return true;
} }
public byte[] getBytes(TString charsetName) throws TUnsupportedEncodingException {
Charset charset = Charset.get(charsetName.toString());
if (charset == null) {
throw new TUnsupportedEncodingException(TString.wrap("Unsupported encoding: " + charsetName));
}
return getBytes(charset);
}
public byte[] getBytes() {
return getBytes(new UTF8Charset());
}
private byte[] getBytes(Charset charset) {
byte[] result = new byte[length() * 2];
int resultLength = 0;
byte[] destArray = new byte[TMath.max(16, TMath.min(length() * 2, 4096))];
ByteBuffer dest = new ByteBuffer(destArray);
CharBuffer src = new CharBuffer(characters);
while (!src.end()) {
charset.encode(src, dest);
if (resultLength + dest.position() > result.length) {
result = TArrays.copyOf(result, result.length * 2);
}
for (int i = 0; i < dest.position(); ++i) {
result[resultLength++] = destArray[i];
}
dest.rewind(0);
}
return TArrays.copyOf(result, resultLength);
}
@Override @Override
public int hashCode() { public int hashCode() {
if (hashCode == 0) { if (hashCode == 0) {

View File

@ -15,6 +15,7 @@
*/ */
package org.teavm.classlib.java.util; package org.teavm.classlib.java.util;
import org.teavm.classlib.java.lang.TMath;
import org.teavm.classlib.java.lang.TObject; import org.teavm.classlib.java.lang.TObject;
/** /**
@ -24,7 +25,16 @@ import org.teavm.classlib.java.lang.TObject;
public class TArrays extends TObject { public class TArrays extends TObject {
public static char[] copyOf(char[] array, int length) { public static char[] copyOf(char[] array, int length) {
char[] result = new char[length]; char[] result = new char[length];
int sz = Math.min(length, array.length); int sz = TMath.min(length, array.length);
for (int i = 0; i < sz; ++i) {
result[i] = array[i];
}
return result;
}
public static byte[] copyOf(byte[] array, int length) {
byte[] result = new byte[length];
int sz = TMath.min(length, array.length);
for (int i = 0; i < sz; ++i) { for (int i = 0; i < sz; ++i) {
result[i] = array[i]; result[i] = array[i];
} }

View File

@ -218,4 +218,56 @@ public class StringTest {
byte[] bytes = { 65, -62, -69, -32, -82, -69, -16, -66, -78, -69 }; byte[] bytes = { 65, -62, -69, -32, -82, -69, -16, -66, -78, -69 };
assertEquals("A\u00BB\u0BBB\uD8BB\uDCBB", new String(bytes, "UTF-8")); assertEquals("A\u00BB\u0BBB\uD8BB\uDCBB", new String(bytes, "UTF-8"));
} }
@Test
public void createFromLongUTF8ByteArray() throws UnsupportedEncodingException {
byte[] bytes = new byte[16384];
for (int i = 0; i < bytes.length;) {
bytes[i++] = -16;
bytes[i++] = -66;
bytes[i++] = -78;
bytes[i++] = -69;
}
String str = new String(bytes, "UTF-8");
assertEquals('\uD8BB', str.charAt(8190));
assertEquals('\uDCBB', str.charAt(8191));
}
@Test
public void getByteArray() throws UnsupportedEncodingException {
byte[] bytes = "123".getBytes("UTF-8");
assertEquals(49, bytes[0]);
assertEquals(50, bytes[1]);
assertEquals(51, bytes[2]);
assertEquals(3, bytes.length);
}
@Test
public void getUTF8ByteArray() throws UnsupportedEncodingException {
byte[] bytes = "A\u00BB\u0BBB\uD8BB\uDCBB".getBytes("UTF-8");
assertEquals(65, bytes[0]);
assertEquals(-62, bytes[1]);
assertEquals(-69, bytes[2]);
assertEquals(-32, bytes[3]);
assertEquals(-82, bytes[4]);
assertEquals(-69, bytes[5]);
assertEquals(-16, bytes[6]);
assertEquals(-66, bytes[7]);
assertEquals(-78, bytes[8]);
assertEquals(-69, bytes[9]);
}
@Test
public void getUTF8ByteArrayOfLongString() throws UnsupportedEncodingException {
char[] chars = new char[8192];
for (int i = 0; i < chars.length;) {
chars[i++] = '\uD8BB';
chars[i++] = '\uDCBB';
}
byte[] bytes = new String(chars).getBytes("UTF-8");
assertEquals(-16, bytes[16380]);
assertEquals(-66, bytes[16381]);
assertEquals(-78, bytes[16382]);
assertEquals(-69, bytes[16383]);
}
} }