简体   繁体   中英

Java write individual bytes to DataOutputStream

I'm working on writing individual bytes to a DataOutputStream in java for an HTTP post request. The post is structured like such:

/* Init Post */
URL PostToLink = new URL(GeneralArguments.get("PostLink_String"));
byte[] PostData = PutKeyedPostArgs.get("PostBody").getBytes("UTF-8");
HttpURLConnection Post_Request_Connection = (HttpURLConnection) PostToLink.openConnection();
Post_Request_Connection.setDoOutput(true);
Post_Request_Connection.setDoInput(false);
Post_Request_Connection.setRequestMethod("POST");
//Post_Request_Connection.setRequestProperty("charset", "utf-8");
Post_Request_Connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
Post_Request_Connection.setRequestProperty("Content-Length", Integer.toString(PostData.length));
Post_Request_Connection.setRequestProperty("Connection", "Keep-Alive");
Post_Request_Connection.setRequestProperty("User-Agent", UserAgent); // Defined earlier
Post_Request_Connection.setRequestProperty("Cookie", CookieVal); // Defined earlier
Post_Request_Connection.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
Post_Request_Connection.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
Post_Request_Connection.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
Post_Request_Connection.setInstanceFollowRedirects(false);
Post_Request_Connection.setUseCaches(false);

/* Obtain Write Stream */
DataOutputStream The_Post_Request_Write_Stream = new DataOutputStream(Post_Request_Connection.getOutputStream());
JOptionPane.showMessageDialog(null, PostData.length); // For Debugging

JOptionPane.showMessageDialog(null, "before for"); // For Debugging
/* Begin writing byte-by-byte to output stream */
for(int CurrentPostByte = 0; CurrentPostByte < PostData.length; CurrentPostByte++){
    JOptionPane.showMessageDialog(null, CurrentPostByte); // For Debugging
    byte[] TemporaryByteArray = new byte[]{PostData[CurrentPostByte]};
    The_Post_Request_Write_Stream.write(TemporaryByteArray, CurrentPostByte, TemporaryByteArray.length);
/* Length should always be 1 */
}

For some reason it after it writes the second byte (the one at PostData[1]) it gets an index out of bounds error. I cannot seem to find out why.

Any clarifications or help is appreciated. Thank you.

Look at your code:

byte[] TemporaryByteArray = new byte[]{PostData[CurrentPostByte]};
The_Post_Request_Write_Stream.write(TemporaryByteArray, CurrentPostByte, TemporaryByteArray.length);

You are passing the array TemporaryByteArray which has a length of 1 (one) to the write method but use the indices valid for the PostData array only.

You may fix the code by changing it to:

byte[] TemporaryByteArray = new byte[]{PostData[CurrentPostByte]};
The_Post_Request_Write_Stream.write(TemporaryByteArray, 0, TemporaryByteArray.length);

or, simpler

byte[] TemporaryByteArray = new byte[]{PostData[CurrentPostByte]};
The_Post_Request_Write_Stream.write(TemporaryByteArray, 0, 1);

or, even simpler

The_Post_Request_Write_Stream.write(PostData, CurrentPostByte, 1);

But, of course, the best solution would be removing the nonsense loop and write the entire array at once, The_Post_Request_Write_Stream.write(PostData); instead of byte by byte.

My educated guesstimate is that the problem is here:

The_Post_Request_Write_Stream.write(TemporaryByteArray, CurrentPostByte, TemporaryByteArray.length);

The parameters to write(byte[], int, int) are:

  • a byte[] buffer which contains the data to be written
  • an int offset that tells where in the buffer the data you want starts
  • an int length which indicates how many bytes you want to write

The problem lies in the value you pass to the offset parameter. It's CurrentPostByte , so in the second iteration you tell it to start reading TemporaryByteArray from index 1 . However, TemporaryByteArray is always a one-element array, it only has one item int it at index 0 .

You should correct that to:

The_Post_Request_Write_Stream.write(TemporaryByteArray, 0, TemporaryByteArray.length);

Or, more simply, to:

The_Post_Request_Write_Stream.write(TemporaryByteArray);

You didn't say which line is throwing the exception, but I'm guessing it's

byte[] TemporaryByteArray = new byte[]{BuyData[CurrentPostByte]};

Looks like you meant to reference PostData here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM