Open GoogleCodeExporter opened 8 years ago
Another bug of here:
int got = _contentStream.read(buf);
int sum = got;
_logger.finest("Got " + got + " bytes");
while (got >= 0) {
sum += got;
here, "int sum = got; " got the wrong place, the first read size will be added
twice.
if variable "sum" is used in future, it should be resolved.
Original comment by harr...@gmail.com
on 21 May 2014 at 8:49
[deleted comment]
[deleted comment]
[deleted comment]
/************here is my implementation of limit length of response content
read ***************/
public byte[] getContent() {
try {
flushContentStream(null);
if (getContentSize() > MessageOutputStream.LARGE_CONTENT_SIZE){
return CONTENT_TOO_BIG;
}
} catch (IOException ioe) {
_logger.info("IOException flushing the contentStream: " + ioe);
}
return decodeContent();
}
/**
* Harry add it, with length limited content read
*/
public byte[] getResponseContent() {
try {
int length = getHeaderInt("Content-Length", -1);
flushContentStream(null, length);
if (getContentSize() > MessageOutputStream.LARGE_CONTENT_SIZE){
return CONTENT_TOO_BIG;
}
} catch (IOException ioe) {
_logger.info("IOException flushing the contentStream: " + ioe);
}
return decodeContent();
}
public byte[] decodeContent() {
if (_content != null && _gzipped) {
try {
InputStream is = getContentInputStream();
....
....
}
public int getHeaderInt(String name, int default_value) {
int ret = default_value;
String value = getHeader(name);
if (value != null) {
try {
ret = Integer.parseInt(value);
} catch (NumberFormatException e) {
_logger.finest("Bad HTTP Header - " + name + ": " + value);
}
}
return ret;
}
/**
* WITH LENGTH LIMITED CONTENT READ
* @param os
* @param length limit length will read from response. It should be >= 0, if length < 0, then no limit length defined
* @throws IOException
*/
private void flushContentStream(OutputStream os, int length) throws IOException {
IOException ioe = null;
if (_contentStream == null) return;
_content = new MessageOutputStream();
byte[] buf = new byte[4096];
_logger.finest("Reading initial bytes from contentStream " + _contentStream);
int got = _contentStream.read(buf, 0, (length >= 0) ? Math.min(length, buf.length) : buf.length);
int sum = got;
_logger.finest("Got " + got + " bytes");
while (got >= 0) {
_content.write(buf,0, got);
if (os != null) {
try {
os.write(buf,0,got);
} catch (IOException e) {
_logger.info("IOException ioe writing to output stream : " + e);
// _logger.info("Had seen " + (_content.size()-got) + " bytes, was writing " + got);
ioe = e;
os = null;
}
}else{
if (LOGD) Log.d(TAG, "output Stream is null");
}
// if length is available(>=0) and read length enough, just return
if (length >= 0 && sum >= length)
break;
got = _contentStream.read(buf, 0, (length >= 0) ? Math.min(length, buf.length) : buf.length);
sum += got;
_logger.finest("Got " + got + " bytes");
}
_content.flush();
_contentStream = null;
if (ioe != null) throw ioe;
}
Original comment by harr...@gmail.com
on 21 May 2014 at 10:00
phu... you are finding a lot of bugs in this flushContentStream() :)
I'm really appreciate.
will look at it and fix it ASAP
Original comment by supp.san...@gmail.com
on 21 May 2014 at 3:17
These bugs are also manifested in real use of code?
Original comment by supp.san...@gmail.com
on 21 May 2014 at 3:19
Original comment by supp.san...@gmail.com
on 21 May 2014 at 3:19
the first one is better to fix, whether read() will return "0" relay to the
implementation of java.util, perhaps in some device the problem will be
triggered?
(as i know, in Async socket connection, read can return 0)
the second one is something to accelerate the return of read().
Original comment by harr...@gmail.com
on 22 May 2014 at 10:29
" sum += got;"
perhaps it should be :
" if (got > 0) sum += got;"
Original comment by harr...@gmail.com
on 22 May 2014 at 10:33
Yes, all this came up if sum is used for deciding when to end.
Don't see any harm also to expect that read returns 0 in some cases.
Will try to incorporate your fixes in code.
Original comment by supp.san...@gmail.com
on 22 May 2014 at 4:20
Original issue reported on code.google.com by
harr...@gmail.com
on 21 May 2014 at 8:42