java - why does "STRING".getBytes() work different according to the Operation System -
i running code below , getting different outcome "some_string".getbytes() depending if in windows or unix. issue happens string (i tried simple abc , same problem.
see differences below printed in console.
the code below well-tested using java 7. if copy entirely run.
additionally, see difference in hexadecimal in 2 images below. first 2 images shows file created in windows. can see hexadecimal values ansi , ebcdic respectively. third image, black one, unix. can see hexadecimal (-c option) , character readable in believe ebcdic.
so, straight question is: why such code work different since using java 7 in both case? should check especific property in somewhere? maybe, java in windows default format , in unix another. if so, property must check or settup?
unix console:
$ ./java -cp /usr/test.jar test.mainframe.read.test.testgetbytes h = 76 - l < wasn't found
windows console:
h = 60 - < h1 = 69 - e h2 = 79 - o h3 = 77 - m h4 = 62 - > end of message found
the entire code:
package test.mainframe.read.test; import java.util.arraylist; public class testgetbytes { public static void main(string[] args) { try { arraylist ipmmessage = new arraylist(); ipmmessage.add(newline()); //windows path writemessage("c:/temp/test_bytes.ipm", ipmmessage); reformatfile("c:/temp/test_bytes.ipm"); //unix path //writemessage("/usr/temp/test_bytes.ipm", ipmmessage); //reformatfile("/usr/temp/test_bytes.ipm"); } catch (exception e) { system.out.println(e.getmessage()); } } public static byte[] newline() { return "<eom>".getbytes(); } public static void writemessage(string filename, arraylist ipmmessage) throws java.io.filenotfoundexception, java.io.ioexception { java.io.dataoutputstream dos = new java.io.dataoutputstream( new java.io.fileoutputstream(filename, true)); (int = 0; < ipmmessage.size(); i++) { try { int[] intvalues = (int[]) ipmmessage.get(i); (int j = 0; j < intvalues.length; j++) { dos.write(intvalues[j]); } } catch (classcastexception e) { byte[] bytevalues = (byte[]) ipmmessage.get(i); dos.write(bytevalues); } } dos.flush(); dos.close(); } // reformat u1014 public static void reformatfile(string filename) throws java.io.filenotfoundexception, java.io.ioexception { java.io.fileinputstream fis = new java.io.fileinputstream(filename); java.io.datainputstream br = new java.io.datainputstream(fis); int h = br.read(); system.out.println("h = " + h + " - " + (char)h); if ((char) h == '<') {// check <eom> int h1 = br.read(); system.out.println("h1 = " + h1 + " - " + (char)h1); int h2 = br.read(); system.out.println("h2 = " + h2 + " - " + (char)h2); int h3 = br.read(); system.out.println("h3 = " + h3 + " - " + (char)h3); int h4 = br.read(); system.out.println("h4 = " + h4 + " - " + (char)h4); if ((char) h1 == 'e' && (char) h2 == 'o' && (char) h3 == 'm' && (char) h4 == '>') { system.out.println("end of message found"); } else{ system.out.println("eom not found < found"); } } else{ system.out.println("< wasn't found"); } } }
you not specifying charset when calling getbytes()
, uses default charset of underlying platform (or of java if specified when java started). stated in string
documentation:
public byte[] getbytes()
encodes string sequence of bytes using platform's default charset, storing result new byte array.
getbytes()
has overloaded version lets specify charset in code.
public byte[] getbytes(charset charset)
encodes string sequence of bytes using given charset, storing result new byte array.
Comments
Post a Comment