Reputation: 79
i have written an application which would test the performance of a servlet. the code that i have written works well with some requests. if i increase to more connections then some times i get "too many bytes written " exception.
int startRange = 0, endRange = 10000;
HttpsURLConnection c = null;
c = getHttpsConnection(URL);
c.setDoInput(true);
c.setDoOutput(true);
c.setRequestProperty("Content-Length",(cotentLength+1));
c.setFixedLengthStreamingMode(contentLength+1);
c.setRequestProperty("Content-Type","appplication/zip");
c.setRequestMethod("POST");
c.setRequestProperty("Content-Range", "bytes "+startRange+"-"+endRange+"/"+filesize);
OutputStream op = c.getOutputStream();
i compute the fixed chunk to send into byte[] partBuf = new byte[contentLength+1];
then
op.write(partBuf)
String range = c.getHeaderField("Range");
in.close();
op.flush();
op.close();
i dont get "too many bytes written error" upto certain requests sent, too many requests sent gives me this error only sometimes .. how do i handle this scenario?
usually for testing, i spawn some requests varying from 10, 100, 500, 1000 .. which inturn hits a servlet that receives the data in chunks.
Upvotes: 1
Views: 10068
Reputation: 16872
I have encountered this java.io.IOException: too many bytes to write to stream
error, and it happened because of non-ASCII characters in the body text. The sending code incorrectly assumed the length of text to be equal to the number of characters. Since the code was a test harness, it was easier to delete these characters than to fix the code.
Upvotes: 1
Reputation: 7351
c.setFixedLengthStreamingMode(x)
requires you to write exactly x
bytes to the stream. From the Javadocs:
An exception will be thrown if the application attempts to write more data than the indicated content-length, or if the application closes the OutputStream before writing the indicated amount.
For example I had a similar problem when I made the following silly mistake:
http.setFixedLengthStreamingMode(body.length());
try (OutputStream os = http.getOutputStream()) {
os.write(body.getBytes("UTF-8"));
} ...
This worked until an accented character appeared in body
. That made the byte length differ from the String length, which led to java.io.IOException: too many bytes written
.
To make matters more confusing, the try-with-resources caused another exception: Suppressed: java.io.IOException: insufficient data written
. I'm still not sure where that came from but it went away when I passed the proper length to setFixedLengthStreamingMode
.
Upvotes: 1
Reputation: 719679
There are a number of typo's in your sample code, so it is unclear what your real code does. However this looks very wrong:
c.setRequestProperty("Content-Range", startRange-endRange/filesize);
The content of that header should be a String, but you seem to be sending an integer value. Here's an example of what the header should look like from the HTTP 1.1 spec.
HTTP/1.1 206 Partial content
Date: Wed, 15 Nov 1995 06:25:24 GMT
Last-Modified: Wed, 15 Nov 1995 04:58:08 GMT
Content-Range: bytes 21010-47021/47022
Content-Length: 26012
Content-Type: image/gif
Your edited version is still incorrect. It should be this:
c.setRequestProperty("Content-Range",
"bytes " + startRange + "-" + endRange + "/" + filesize);
... assuming that those variables have the appropriate values.
Now that you have fixed those errors, my next point of suspicion is the values themselves ... and the contentLength
value.
And if that isn't the answer, consider the possibility that the server or servlet does not support "Content-Range" for POST.
Upvotes: 1
Reputation: 311054
You have set the wrong content-length value. There is no need to set it at all: Java does that for you. Just remove the code.
Upvotes: 0