JDK-6400767 : Method for binary data in strings
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0
  • Priority: P5
  • Status: Closed
  • Resolution: Not an Issue
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-03-20
  • Updated: 2012-01-11
  • Resolved: 2006-07-21
Related Reports
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
Because Sun Microsystems deprecated the following methods (of the java.lang.String class):

1. String(byte[] ascii, int hibyte)
2. String(byte[] ascii, int hibyte, int offset, int count)
3. void getBytes(int srcBegin, int srcEnd, byte[] dst, int dstBegin)

it is inconvenient to use the java.lang.String class to store binary data, though this is often a useful thing to do.

JUSTIFICATION :
Using java.lang.String is useful because unlike with byte[], one can use methods such as indexOf, to search through data.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Methods specified above should not be deprecated or some other means of allowing binary data in java.lang.String (i.e. with a new Charset called "BINARY")

ACTUAL -
Instead, to read binary data into a java.lang.String, one must use a loop (or the deprecated methods).  Writing binary data is ok (using ok in the "not entirely pleasing" sense, because the DataOutputStream.writeBytes (String) handles this fairly well, at least for writing to output streams).

CUSTOMER SUBMITTED WORKAROUND :
byte[] input...
...
String readString = new String (input, 0);

Comments
EVALUATION Users can pretend that the "ISO-8859-1" charset is the desired "BINARY" charset. It provides a 1-1 mapping between bytes and chars less than 0x100. new String(bytes, "ISO-8859-1") This should be closed as Not a Defect. We might, perhaps, consider adding an alias like "binary" for "ISO-8859-1", as Emacs does.
2006-03-20