Note that there are some explanatory texts on larger screens.

plurals
  1. POwrting byte array in java
    text
    copied!<p>I'm trying to realize a Java server / c client and I have a problem with my put method in java. The server receive for example the array buffer. I printed in server what it received and it was = {-17 -65 -67 -17 -65 -67 -17 -65 -67 -17 -65 -67 0 16 74 70 73 70 0 1 1 0 0 1 0}. but when I write it with </p> <pre><code>FileOutputStream out = new FileOutputStream(filename); out.write(buffer); </code></pre> <p>And then I read the same file with </p> <pre><code>FileInputStream is = new FileInputStream(filename); is.read(buffer); </code></pre> <p>I don't get the same byte array. in this Example I get : buffer = {-17 -65 -67 -17 -65 -67 -17 -65 -67 -17 -65 -67 0 16 74 70 73 1 0 0 -1 -2 0 59 67}</p> <p><strong>EDIT :</strong> </p> <pre><code>private void put(String request, InputStream entreeSocket, OutputStream sortieSocket) { //request = "a/b/c/file.txt"; System.out.println(request); String[] s = request.split(" "); Path path = Paths.get(s[0]); String filename =""; int i = s[0].indexOf('/'); //System.out.println(request+i); if (i != -1){//System.out.println(request.substring(i)); filename = s[0].substring(s[0].lastIndexOf('/')+1); s[0] = s[0].substring(0,s[0].lastIndexOf('/')); path = Paths.get(s[0]); filename = path + "/"+filename; if (Files.notExists(path)){ if (!new File(s[0]).mkdirs()) return; } //System.out.println(path + " " + filename); } else filename = s[0]; i = request.indexOf("Content-Length"); //System.out.println(request.substring(i)); i = request.indexOf(":",i); //System.out.println(request.substring(i)); int j = request.indexOf("\r\n",i); int length = Integer.valueOf(request.substring(i+1,j).trim()); //System.out.print(length); byte[] buffer = new byte[2048]; j = request.indexOf("\r\n\r\n"); request = request.substring(j+4); System.out.print(length); //length = length - request.length(); try { buffer = request.getBytes("UTF-8"); print(buffer,buffer.length); length -= request.getBytes("UTF-8").length; FileOutputStream out = new FileOutputStream(filename); out.write(buffer, 0, request.getBytes("UTF-8").length); length = 0; while(length &gt; 0){ int nb; if(length &gt; buffer.length) nb = entreeSocket.read(buffer,0,buffer.length); else nb = entreeSocket.read(buffer,0,length); print(buffer,nb); out.write(buffer,0,nb); if(nb == -1) break; length -=nb; } sortieSocket.write(new String("205 OK\r\n").getBytes()); out.close(); entreeSocket.close(); sortieSocket.close(); FileInputStream is = new FileInputStream(filename); int nb = is.read(buffer); print(buffer,nb); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } </code></pre> <p>That the debut of my corrupt files:</p> <pre><code>ef bf bd 50-4e 47 0d 0a-1a 0a 00 00-00 0d 49 48 </code></pre> <p>That the debut of the original files</p> <pre><code>89 50 4e 47-0d 0a 1a 0a-00 00 00 0d-49 48 44 52 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload