Changes for charset encode/decode

This commit is contained in:
Jgunishka 2019-08-21 21:31:30 +03:00
parent c8958d7b12
commit ebadbb4538

View File

@ -254,28 +254,29 @@ public class EmuUtil
public static String readString(ByteBuffer buffer, int stopByte, Charset charset) public static String readString(ByteBuffer buffer, int stopByte, Charset charset)
{ {
//ByteBuffer tempBuffer = ByteBuffer.allocate(buffer.remaining()); ByteBuffer tempBuffer = ByteBuffer.allocate(buffer.remaining());
char[] tempArray = new char[buffer.remaining()]; // char[] tempArray = new char[buffer.remaining()];
byte b; // byte b;
int i; // int i;
// while (buffer.hasRemaining()) while (buffer.hasRemaining())
for(i=0; i<tempArray.length; i++) // for(i=0; i<tempArray.length; i++)
{ {
byte b;
if ((b = buffer.get()) == stopByte) if ((b = buffer.get()) == stopByte)
break; break;
// tempBuffer.put(b); tempBuffer.put(b);
tempArray[i] = (char)b; // tempArray[i] = (char)b;
} }
// return charset.decode((ByteBuffer) tempBuffer.flip()).toString(); return charset.decode((ByteBuffer) tempBuffer.flip()).toString();
return new String(tempArray, 0, i); // return new String(tempArray, 0, i);
} }
public static void writeString(ByteBuffer buffer, String s, int stopByte, Charset charset) public static void writeString(ByteBuffer buffer, String s, int stopByte, Charset charset)
{ {
// buffer.put(charset.encode(s)); buffer.put(charset.encode(s));
char[] tempArray = s.toCharArray(); // char[] tempArray = s.toCharArray();
for(int i=0; i<tempArray.length; i++) // for(int i=0; i<tempArray.length; i++)
buffer.put((byte) tempArray[i]); // buffer.put((byte) tempArray[i]);
buffer.put((byte) stopByte); buffer.put((byte) stopByte);
} }