File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Buffer size or other? Please help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Buffer size or other? Please help" Watch "Buffer size or other? Please help" New topic
Author

Buffer size or other? Please help

Hongqing Lu
Greenhorn

Joined: Jan 14, 2004
Posts: 13
TestServlet runs in Tomcat, it try to get data from socket server then response to client.
It runs well when the data is small. However it will crash when the data is big enough(about 8k). The break point is in the while loop when reading data from BufferedReader in method doSendRequestTCN2.
I have tyied several methods, but all faild.
I have no idea about it.
Is it caused by buffer size or other?
Please help!!!
Sample source code:
public class TestServlet extends HttpServlet {
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
}
public void service(HttpServletRequest req,
HttpServletResponse resp) throws ServletException,
IOException {
try {
PrintWriter out = resp.getWriter();
// get response from socket server
String response = Test.doSendRequestTCN2(requestString);
out.println(response);
} catch (Exception ex) {
ex.printStackTrace();
throw new ServletException("Exception in service method: " +
ex.getMessage());
}
}
}
class Test{
public static String doSendRequestTCN2(String input){
StringBuffer fileLines = new StringBuffer();
String response = null;
try{
client = new Socket(host_ip, host_port);

socketWriter = new PrintWriter(client.getOutputStream());
// get stream from socket server
socketReader = new BufferedReader(new InputStreamReader(client.getInputStream()));
socketWriter.println(input);
socketWriter.flush();
String line = null;
int i = 0;
int sizeOfBuffer = 100;
char[] cbuf = new char[sizeOfBuffer];

int chars = 0;
// read from buffered stream
while ( (chars = socketReader.read(cbuf)) >= 0) {
fileLines.append(cbuf, 0, chars);
i+=1;
}
response = fileLines.toString();
socketWriter.close();
socketReader.close();
client.close();
}
catch (Exception e) {
e.printStackTrace();
throw new WMSException(e);
}
return response;
}
}
Please help!
Thanks in advance!!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Buffer size or other? Please help