我有一個HTTP請求調度程序類,大多數情況下都能正常工作,但我注意到它在收到更大的請求時「停止」。在研究這個問題後,我想我可能沒有爲緩衝區分配足夠的字節。之前,我是這樣做的:Java HTTP請求緩衝區大小
byte[] buffer = new byte[10000];
將其更改爲20000後,它似乎已經停止拖延:
String contentType = connection.getHeaderField("Content-type");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream responseData = connection.openInputStream();
byte[] buffer = new byte[20000];
int bytesRead = responseData.read(buffer);
while (bytesRead > 0) {
baos.write(buffer, 0, bytesRead);
bytesRead = responseData.read(buffer);
}
baos.close();
connection.close();
我這樣做對嗎?無論如何,我可以根據請求的大小動態設置緩衝區的字節數?
謝謝...
感謝您的建議! – littleK 2010-04-16 13:52:58
不客氣。 – BalusC 2010-04-16 13:55:35