United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
JDK-6400767 : Method for binary data in strings

Details
Type:
Enhancement
Submit Date:
2006-03-20
Status:
Closed
Updated Date:
2012-01-11
Project Name:
JDK
Resolved Date:
2006-07-21
Component:
core-libs
OS:
windows_xp
Sub-Component:
java.lang
CPU:
x86
Priority:
P5
Resolution:
Not an Issue
Affected Versions:
5.0
Fixed Versions:

Related Reports
Relates:

Sub Tasks

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



Hardware and Software, Engineered to Work Together