Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.server;
 
 import java.io.Writer;
 
 
Output.

Implements javax.servlet.ServletOutputStream from the javax.servlet package.

A javax.servlet.ServletOutputStream implementation that writes content to a org.eclipse.jetty.http.AbstractGenerator. The class is designed to be reused and can be reopened after a close.
 
 public class HttpOutput extends ServletOutputStream 
 {
     protected final AbstractHttpConnection _connection;
     protected final AbstractGenerator _generator;
     private boolean _closed;
     private ByteArrayBuffer _onebyte;
     
     // These are held here for reuse by Writer
     Writer _converter;
     char[] _chars;
 
     /* ------------------------------------------------------------ */
     public HttpOutput(AbstractHttpConnection connection)
     {
         =connection;
         =(AbstractGenerator)connection.getGenerator();
     }
 
     /* ------------------------------------------------------------ */
     public int getMaxIdleTime()
     {
         return .getMaxIdleTime();
     }
     
     /* ------------------------------------------------------------ */
     public boolean isWritten()
     {
         return .getContentWritten()>0;
     }
     
     /* ------------------------------------------------------------ */
     /*
      * @see java.io.OutputStream#close()
      */
     @Override
     public void close() throws IOException
     {
         =true;
     }
     
     /* ------------------------------------------------------------ */
     public boolean isClosed()
     {
         return ;
     }
     
     /* ------------------------------------------------------------ */
     public void reopen()
     {
         =false;
     }
     
     /* ------------------------------------------------------------ */
     @Override
     public void flush() throws IOException
     {
        .flush(getMaxIdleTime());
    }
    /* ------------------------------------------------------------ */
    @Override
    public void write(byte[] bint offint lenthrows IOException
    {
        write(new ByteArrayBuffer(b,off,len));
    }
    /* ------------------------------------------------------------ */
    /*
     * @see java.io.OutputStream#write(byte[])
     */
    @Override
    public void write(byte[] bthrows IOException
    {
        write(new ByteArrayBuffer(b));
    }
    
    /* ------------------------------------------------------------ */
    /*
     * @see java.io.OutputStream#write(int)
     */
    @Override
    public void write(int bthrows IOException
    {
        if (==null)
            =new ByteArrayBuffer(1);
        else
            .clear();
        .put((byte)b);
        write();
    }
    /* ------------------------------------------------------------ */
    private void write(Buffer bufferthrows IOException
    {
        if ()
            throw new IOException("Closed");
        if (!.isOpen())
            throw new EofException();
        
        // Block until we can add _content.
        while (.isBufferFull())
        {
            .blockForOutput(getMaxIdleTime());
            if ()
                throw new IOException("Closed");
            if (!.isOpen())
                throw new EofException();
        }
        // Add the _content
        .addContent(buffer.);
        // Have to flush and complete headers?
        
        if (.isAllContentWritten())
        {
            flush();
            close();
        } 
        else if (.isBufferFull())
            .commitResponse(.);
        // Block until our buffer is free
        while (buffer.length() > 0 && .isOpen())
        {
            .blockForOutput(getMaxIdleTime());
        }
    }
    /* ------------------------------------------------------------ */
    /* 
     * @see javax.servlet.ServletOutputStream#print(java.lang.String)
     */
    @Override
    public void print(String sthrows IOException
    {
        write(s.getBytes());
    }
New to GrepCode? Check out our FAQ X