A DESCRIPTION OF THE REQUEST :
Class java.nio.ByteBuffer is often used to process native zero-terminated ASCII strings.
If ByteBuffer wraps from a fixed sized byte array there are probably trailing 0x00's and neither position nor limit are correctly aligned to these. Then the ByteBuffer's limit must be aligned to the first 0x00.
For this I suggest a method, which can be named:
ByteBuffer limitTo(byte terminator)
ByteBuffer terminateTo(byte terminator)
ByteBuffer flipTo(byte terminator)
JUSTIFICATION :
- zero-terminated ASCII strings are often used to access native APIs.
- is more elegant than extra lines of code
- can be chained directly into expression
- helps to fix bug 6449421
See Forum: http://forums.java.net/jive/thread.jspa?messageID=135359𡂿
---------- BEGIN SOURCE ----------
Usage :
(Possible fix for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6449421)
return Charset.forName(charSet).newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.replaceWith("?")
.decode (ByteBuffer.wrap(inBytes).limitAt(0))
.toString();
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
int len = 0;
while (inBytes[len] != 0)
len++;
return Charset.forName(charSet).newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.replaceWith("?")
.decode (ByteBuffer.wrap(inBytes, 0, len))
.toString();
Class java.nio.ByteBuffer is often used to process native zero-terminated ASCII strings.
If ByteBuffer wraps from a fixed sized byte array there are probably trailing 0x00's and neither position nor limit are correctly aligned to these. Then the ByteBuffer's limit must be aligned to the first 0x00.
For this I suggest a method, which can be named:
ByteBuffer limitTo(byte terminator)
ByteBuffer terminateTo(byte terminator)
ByteBuffer flipTo(byte terminator)
JUSTIFICATION :
- zero-terminated ASCII strings are often used to access native APIs.
- is more elegant than extra lines of code
- can be chained directly into expression
- helps to fix bug 6449421
See Forum: http://forums.java.net/jive/thread.jspa?messageID=135359𡂿
---------- BEGIN SOURCE ----------
Usage :
(Possible fix for http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6449421)
return Charset.forName(charSet).newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.replaceWith("?")
.decode (ByteBuffer.wrap(inBytes).limitAt(0))
.toString();
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
int len = 0;
while (inBytes[len] != 0)
len++;
return Charset.forName(charSet).newDecoder()
.onMalformedInput(CodingErrorAction.REPLACE)
.onUnmappableCharacter(CodingErrorAction.REPLACE)
.replaceWith("?")
.decode (ByteBuffer.wrap(inBytes, 0, len))
.toString();