dev@glassfish.java.net

Re: Servlet with fileresume download...

From: charlie hunt <charlie.hunt_at_sun.com>
Date: Fri, 28 Dec 2007 11:05:11 -0600

Hi Dyego,

The stack trace and java.lang.OutOfMemoryError is indicating you are out
of Java heap space. Try increasing the size of the Java heap (-Xmx) in
GlassFish's domain.xml file.

If that only prolongs an eventual java.lang.OutOfMemoryError, then we
may have to further investigate.

hths,

charlie ...

Dyego Souza do Carmo wrote:
> I get a error if my Download Accelerator Get (six) connections to
> download using my servlet...
>
> Any Help ?
>
> the error is:
>
>
> [#|2007-12-28T07:59:29.258-0300|WARNING|sun-appserver9.1|javax.enterprise.system.stream.err|_ThreadID=26;_ThreadName=httpSSLWorkerThread-8080-4;_RequestID=bc0c0840-f05a-43f1-a4d3-baf26b936ffd;|java.lang.OutOfMemoryError:
> Java heap space
> at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:39)
> at java.nio.ByteBuffer.allocate(ByteBuffer.java:312)
> at
> com.sun.enterprise.web.connector.grizzly.SocketChannelOutputBuffer.realWriteBytes(SocketChannelOutputBuffer.java:130)
>
> at
> org.apache.coyote.http11.InternalOutputBuffer$OutputStreamOutputBuffer.doWrite(InternalOutputBuffer.java:851)
>
> at
> org.apache.coyote.http11.filters.IdentityOutputFilter.doWrite(IdentityOutputFilter.java:141)
>
> at
> org.apache.coyote.http11.InternalOutputBuffer.doWrite(InternalOutputBuffer.java:626)
>
> at org.apache.coyote.Response.doWrite(Response.java:599)
> at
> org.apache.coyote.tomcat5.OutputBuffer.realWriteBytes(OutputBuffer.java:404)
>
> at
> org.apache.tomcat.util.buf.ByteChunk.flushBuffer(ByteChunk.java:417)
> at
> org.apache.coyote.tomcat5.OutputBuffer.doFlush(OutputBuffer.java:357)
> at
> org.apache.coyote.tomcat5.OutputBuffer.close(OutputBuffer.java:320)
> at
> org.apache.coyote.tomcat5.CoyoteResponse.finishResponse(CoyoteResponse.java:577)
>
> at
> org.apache.coyote.tomcat5.CoyoteAdapter.afterService(CoyoteAdapter.java:316)
>
> at
> com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.postResponse(DefaultProcessorTask.java:582)
>
> at
> com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:569)
>
> at
> com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:813)
>
> at
> com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:339)
>
> at
> com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:261)
>
> at
> com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:212)
>
> at
> com.sun.enterprise.web.portunif.PortUnificationPipeline$PUTask.doTask(PortUnificationPipeline.java:361)
>
> at
> com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265)
> at
> com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
>
>
>
> Code of my servlet is:
>
> public class ResumeDownload extends HttpServlet {
>
> /**
> * Processes requests for both HTTP <code>GET</code> and
> <code>POST</code> methods.
> * @param request servlet request
> * @param response servlet response
> * @throws javax.servlet.ServletException
> */
> protected void processRequest(HttpServletRequest request,
> HttpServletResponse response)
> throws ServletException, IOException {
>
> String rangeStr = request.getHeader("Range");
> if (rangeStr == null) {
> rangeStr = request.getHeader("range");
> }
>
>
> System.out.println("Veja o Range !!!! "+ rangeStr);
>
>
> File arquivo = new File("/jose.zip");
> FileInputStream arqStream = new FileInputStream(arquivo);
>
>
> long startPosition = 0;
> long finalPosition = 0;
> long totalLength = arquivo.length();
>
> if (rangeStr == null) {
> finalPosition = totalLength - 1;
> } else {
> String rangeStrApenasRange = new StringTokenizer(rangeStr,
> "bytes=").nextToken();
>
> response.setStatus(206);
>
> long finalLineInformation = 0;
> long inititalLineInformation = 0;
>
> /* Posição Final informada */
> StringTokenizer strT = new
> StringTokenizer(rangeStrApenasRange, "-");
> if (rangeStrApenasRange.indexOf("-") == 0) {
> inititalLineInformation = -1;
> finalLineInformation = Integer.valueOf(strT.nextToken());
> } else {
> inititalLineInformation =
> Integer.valueOf(strT.nextToken());
> if (strT.countTokens() == 2) {
> finalLineInformation =
> Integer.valueOf(strT.nextToken());
> } else {
> finalLineInformation = -1;
> }
> }
>
> if (inititalLineInformation == -1) {
> finalPosition = totalLength - 1;
> startPosition = finalPosition;
> startPosition -= finalLineInformation;
> } else if (finalLineInformation == -1) {
> finalPosition = totalLength - 1;
> startPosition = inititalLineInformation;
> } else {
> startPosition = inititalLineInformation;
> finalPosition = finalLineInformation;
> }
>
> }
>
> long readPosition = startPosition;
> long lengthOfData = (finalPosition+1) - startPosition;
>
>
>
> StringBuffer contentDisposition = new StringBuffer();
> contentDisposition.append("attachment;");
> contentDisposition.append("filename=\"");
> contentDisposition.append("jose.zip");
> //contentDisposition.
> contentDisposition.append("\"");
> response.setHeader("Content-Disposition",
> contentDisposition.toString());
> response.setHeader("Accept-Ranges", "bytes");
> response.setHeader("Content-Range", "bytes " + startPosition +
> "-" + finalPosition + "/" + totalLength);
> response.setHeader("Content-Length",
> String.valueOf(lengthOfData));
> response.setContentType("application/x-octetstream");
>
> System.out.println("Eu mandei um range de "+"bytes " +
> startPosition + "-" + finalPosition + "/" + totalLength);
> System.out.println("E um contentLength de
> "+String.valueOf(lengthOfData));
>
>
>
> ServletOutputStream output = response.getOutputStream();
> byte[] buffer = new byte[1024];
> int n = 0;
> arqStream.skip(startPosition);
> while ((n = arqStream.read(buffer)) > 0) {
> output.write(buffer);
> readPosition += 1024;
> if (readPosition >= finalPosition) {
> break;
> }
> }
> arqStream.close();
> System.out.println("Veja só , acabei !!! "+rangeStr);
> output.close();
>
> }
>
>
> // <editor-fold defaultstate="collapsed" desc="HttpServlet methods.
> Click on the + sign on the left to edit the code.">
> /**
> * Handles the HTTP <code>GET</code> method.
> * @param request servlet request
> * @param response servlet response
> */
> protected void doGet(HttpServletRequest request,
> HttpServletResponse response)
> throws ServletException, IOException {
> processRequest(request, response);
> }
>
> /**
> * Handles the HTTP <code>POST</code> method.
> * @param request servlet request
> * @param response servlet response
> */
> protected void doPost(HttpServletRequest request,
> HttpServletResponse response)
> throws ServletException, IOException {
> processRequest(request, response);
> }
>
> /**
> * Returns a short description of the servlet.
> */
> public String getServletInfo() {
> return "Short description";
> }
> // </editor-fold>
> }
>
>

-- 
Charlie Hunt
Java Performance Engineer
<http://java.sun.com/docs/performance/>