0

Suppose i have a byte array need to be converted to a char array and send over the network. eg

char[] sometoken = String( myByteArray ).toCharArray();
myMethodToSendByteArrayUsingTCPSocket( sometoken );

myByteArray is ASN.1 encoding (is a kerberos TGT) . At the receiving end, I am using this code to write "sometoken" to a file.

   try{
        fileName = .....;
        fs =  new FileOutputStream(fileName)  ;
        fs.write( new String( sometoken ).getBytes() );         
        fs.flush();
        fs.close(); 
    }catch(IOException e){
        e.printStackTrace();
    }

May I ask, if I should use DataOutputStream to write to file for reliability? Also, I believe need to specify the ASN.1 encoding for getBytes(). Is it getBytes("ASN.1") ?

thanks.

dorothy
  • 1,213
  • 5
  • 20
  • 35
  • 3
    "Suppose i have a byte array need to be converted to a char array" -- there's your first mistake. By the time you've converted an array of arbitrary bytes to String you've hopelessly corrupted the data. – Hot Licks Jul 17 '14 at 15:42
  • @HotLicks, but I need to pass char[] to the myMethodToSendByteArrayUsingTCPSocket() method. It accepts only char[] arguments. thanks – dorothy Jul 17 '14 at 15:50
  • Back up. What is the data you're sending? Where did it come from? What does it look like? And who is on the other end receiving the data? – Hot Licks Jul 17 '14 at 15:59
  • @HotLicks, i have stated its a kerberos ticket. need to send this ticket to some other server but need to convert to char[] first. After that, i want to get back the ticket with original encoding. Kerberos ticket is created using getEncoded() from KerberosTicket module. getEncoded() generate ASN.1 encoded ticket. thanks – dorothy Jul 17 '14 at 16:05
  • `String( myByteArray )` should be `new String( myByteArray, encoding )`. ASN.1 is not a character encoding. You can't write `getBytes("ASN.1")`. You have to use a Java ASN.1 library. Also, a char[] has UTF-16BE characters in it. Is that actually what your wire protocol requires? – David Conrad Jul 17 '14 at 16:13
  • I missed this: "myByteArray is ASN.1 encoding." You need to use an ASN.1 library to parse it, you can't just convert it to a String. – David Conrad Jul 17 '14 at 16:15
  • From what I see, ASN.1 is "binary" data, and cannot be "losslessly" converted to/from character data. – Hot Licks Jul 17 '14 at 16:16
  • (Do note that there's a significant difference in meaning between C's "char" and Java's. In C parlance a "char array" may be character data, or may be "pure binary" -- 8 bits per char in either case. In Java a "char array" is an array of 16-bit UTF16 characters, period.) – Hot Licks Jul 17 '14 at 16:19
  • @HotLicks A `char` array is an arbitrary sequence of UTF-16 code-units. (A `String` is a sequence of UTF-16 code-units that comprise valid UTF-16 codepoints.) – Tom Blodget Jul 18 '14 at 03:41
  • Hi, my main purpose is only to save this ticket and use it on other machines. http://stackoverflow.com/questions/24817322/how-to-save-kerberos-private-credentials-for-use-in-other-machines – dorothy Jul 18 '14 at 10:06
  • @TomBlodget - Actually, I don't believe that there's any check that a String contains only valid UTF16 codepoints. But the point is that you have to go out of your way to use a Java `char` array for "binary", whereas with C it's done all the time (and there really isn't another option). So reading an interface document that is written from a C standpoint will often use terminology that is essentially nonsense for Java. – Hot Licks Jul 18 '14 at 11:22

2 Answers2

2

This happens all the time in the real world. Anytime you look at an image in a browser, you're looking at a string of byte data sent over a socket. Just BASE64 encode your data, which will convert the bytes to "printable" ASCII text. Then transmit that, and reverse at the far end.

Greycon
  • 793
  • 6
  • 19
  • There are ways to transmit the data without Base64 encoding it. And the two ends have to agree if the Base64 scheme is to be used. – Hot Licks Jul 17 '14 at 16:31
  • your description is not correct (you can send binary data over a socket without converting it to string data, in fact, this is prefereable), however your answer for this question is correct (use Base64 encoding). – jtahlborn Jul 17 '14 at 16:34
0

You can use the "CP437" Charset to round-trip data without data loss. It has 256 characters with one byte per character. As others point out, both sides have to agree.

On the other hand, byteArray is really what should be sent. And since you are saving to a file first, just use java.nio.files.Files.write(Path path, byte[] bytes, OpenOption... options).

Tom Blodget
  • 20,260
  • 3
  • 39
  • 72