简体   繁体   English

Java将单个字节写入DataOutputStream

[英]Java write individual bytes to DataOutputStream

I'm working on writing individual bytes to a DataOutputStream in java for an HTTP post request. 我正在努力将单个字节写入java中的DataOutputStream以获取HTTP post请求。 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. 由于某种原因,它在写入第二个字节(PostData [1]中的一个字节)之后,它得到一个索引越界错误。 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. 您将具有长度1 (一)的数组TemporaryByteArray传递给write方法,但仅使用对PostData数组有效的索引。

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); 但是,当然,最好的解决方案是删除无意义循环并立即写入整个数组, 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: write(byte[], int, int)的参数write(byte[], int, int)是:

  • a byte[] buffer which contains the data to be written 一个byte[] 缓冲区 ,包含要写入的数据
  • an int offset that tells where in the buffer the data you want starts 一个int 偏移量 ,它告诉缓冲区中您想要的数据从何处开始
  • an int length which indicates how many bytes you want to write 一个int 长度 ,表示要写入的字节数

The problem lies in the value you pass to the offset parameter. 问题在于传递给offset参数的值。 It's CurrentPostByte , so in the second iteration you tell it to start reading TemporaryByteArray from index 1 . 它是CurrentPostByte ,所以在第二次迭代中,你告诉它开始从索引1读取TemporaryByteArray However, TemporaryByteArray is always a one-element array, it only has one item int it at index 0 . 但是, TemporaryByteArray始终是一个单元素数组,它在索引0处只有一个int项。

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. 看起来你想在这里引用PostData。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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