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)
{
//ByteBuffer tempBuffer = ByteBuffer.allocate(buffer.remaining());
char[] tempArray = new char[buffer.remaining()];
byte b;
int i;
// while (buffer.hasRemaining())
for(i=0; i<tempArray.length; i++)
ByteBuffer tempBuffer = ByteBuffer.allocate(buffer.remaining());
// char[] tempArray = new char[buffer.remaining()];
// byte b;
// int i;
while (buffer.hasRemaining())
// for(i=0; i<tempArray.length; i++)
{
byte b;
if ((b = buffer.get()) == stopByte)
break;
// tempBuffer.put(b);
tempArray[i] = (char)b;
tempBuffer.put(b);
// tempArray[i] = (char)b;
}
// return charset.decode((ByteBuffer) tempBuffer.flip()).toString();
return new String(tempArray, 0, i);
return charset.decode((ByteBuffer) tempBuffer.flip()).toString();
// return new String(tempArray, 0, i);
}
public static void writeString(ByteBuffer buffer, String s, int stopByte, Charset charset)
{
// buffer.put(charset.encode(s));
char[] tempArray = s.toCharArray();
for(int i=0; i<tempArray.length; i++)
buffer.put((byte) tempArray[i]);
buffer.put(charset.encode(s));
// char[] tempArray = s.toCharArray();
// for(int i=0; i<tempArray.length; i++)
// buffer.put((byte) tempArray[i]);
buffer.put((byte) stopByte);
}