Deserializer { public String readString() { _detectBufferUnderflow(5); boolean asciiCode = BigEndianCodec.getBoolean(_buffer, _index++); int length = BigEndianCodec.getInt(_buffer, _index); _index += 4; if (asciiCode) { _detectBufferUnderflow(length); String s = new String(_buffer, _index, length); _index += length; return s; } length <<= 1; _detectBufferUnderflow(length); ByteBuffer byteBuffer = ByteBuffer.wrap(_buffer, _index, length); _index += length; CharBuffer charBuffer = byteBuffer.asCharBuffer(); return charBuffer.toString(); } Deserializer(ByteBuffer byteBuffer); boolean readBoolean(); byte readByte(); char readChar(); double readDouble(); float readFloat(); int readInt(); long readLong(); T readObject(); short readShort(); String readString();  }
@Test public void testReadString() { String asciiString = "abcdefghijklmn"; byte[] buffer = new byte[asciiString.length() + 5]; buffer[0] = 1; BigEndianCodec.putInt(buffer, 1, asciiString.length()); for (int i = 0; i < asciiString.length(); i++) { buffer[5 + i] = (byte)asciiString.charAt(i); } ByteBuffer byteBuffer = ByteBuffer.wrap(buffer); Deserializer deserializer = new Deserializer(byteBuffer); String resultString = deserializer.readString(); Assert.assertEquals(asciiString, resultString); String nonasciiString = "非ASCII Code中文测试"; buffer = new byte[(nonasciiString.length() * 2) + 5]; buffer[0] = 0; BigEndianCodec.putInt(buffer, 1, nonasciiString.length()); for (int i = 0; i < nonasciiString.length(); i++) { BigEndianCodec.putChar( buffer, 5 + (i * 2), nonasciiString.charAt(i)); } byteBuffer = ByteBuffer.wrap(buffer); deserializer = new Deserializer(byteBuffer); resultString = deserializer.readString(); Assert.assertEquals(nonasciiString, resultString); }