title: Java byte array contains negative numbers
body: <p>I'm reading a file into a byte array in chunks and sending it over the network via a POST request to a webserver. It's not anything complicated, I've done it before using this exact same code. This time, I noticed that my images are looking really odd when they get to the server, so I decided to look at the byte array being sent and the one being received just to make sure it was the same. It's not. On the java sending side the byte array contains negative numbers. On the C# receiving side, there are no negative numbers.</p>
  
  <p>The first 15 bytes on the receiving side (C#)</p>
  
  <pre><code>137
  80
  78
  71
  13
  10
  26
  10
  0
  0
  0
  13
  73
  72
  68
  </code></pre>
  
  <p>Those same bytes but on the sending side (java)</p>
  
  <pre><code>-119
  80
  78
  71
  13
  10
  26
  10
  0
  0
  0
  13
  73
  72
  68
  </code></pre>
  
  <p>All of the non-negative numbers are the same, and the -119 isn't the only negative number, they are all over. I did notice that -119 and 137 are 256 apart and wondered if that has something to do with it.</p>
  
  <p>The code I'm using to read the image (java)</p>
  
  <pre><code>public static byte[] readPart(String fileName, long offset, int length) throws FileNotFoundException, Exception
  {
      byte[] data = new byte[length];
      File file = new File(fileName);
      InputStream is = new FileInputStream(file);
      is.skip(offset);
      is.read(data,0,data.length);
      is.close();
      return data;
  }
  </code></pre>
  
  <p>The code I'm using to write the data (c#)</p>
  
  <pre><code>    private void writeFile(string fileName, Stream contents)
      {
          using (FileStream fs = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
          {
              int bufferLen = 65000;
              byte[] buffer = new byte[bufferLen];
              int count = 0;
              while ((count = contents.Read(buffer, 0, bufferLen)) &gt; 0)
              {
                  fs.Write(buffer, 0, count);
              }
              fs.Close();
          }
          contents.Close();
      }
  </code></pre>
  
  <p>I don't know if that is something that always happens and I just never noticed it before or if it is something that decided to go horribly wrong. What I do know is that this code worked before for something very similar and that it's not working now.</p>
  
  <p>If anyone has any suggestions or an explanation I would really appreciate it.</p>
  
  <p>EDIT:
  The reason my images were looking odd is how I was calling the readPart method.</p>
  
  <pre><code>byte[] data = FileUtilities.readPart(fileName,counter,maxFileSize);//counter is the current chunk number
  </code></pre>
  
  <p>How I should have been calling it</p>
  
  <pre><code>byte[] data = FileUtilities.readPart(fileName,counter*maxFileSize,maxFileSize);//the current chunk * cuhnksize for the offset...
  </code></pre>
  
  <p>Thanks everyone, I'm significantly less confused now :)</p>