View Javadoc

1   // ========================================================================
2   // Copyright (c) 2008-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.server;
15  
16  import java.io.IOException;
17  import java.io.Writer;
18  
19  import javax.servlet.ServletOutputStream;
20  
21  import org.eclipse.jetty.http.AbstractGenerator;
22  import org.eclipse.jetty.http.Generator;
23  import org.eclipse.jetty.io.Buffer;
24  import org.eclipse.jetty.io.ByteArrayBuffer;
25  import org.eclipse.jetty.io.EofException;
26  import org.eclipse.jetty.util.ByteArrayOutputStream2;
27  
28  /** Output.
29   * 
30   * <p>
31   * Implements  {@link javax.servlet.ServletOutputStream} from the {@link javax.servlet} package.   
32   * </p>
33   * A {@link ServletOutputStream} implementation that writes content
34   * to a {@link AbstractGenerator}.   The class is designed to be reused
35   * and can be reopened after a close.
36   */
37  public class HttpOutput extends ServletOutputStream 
38  {
39      protected final AbstractGenerator _generator;
40      protected final long _maxIdleTime;
41      protected final ByteArrayBuffer _buf = new ByteArrayBuffer(AbstractGenerator.NO_BYTES);
42      protected boolean _closed;
43      
44      // These are held here for reuse by Writer
45      String _characterEncoding;
46      Writer _converter;
47      char[] _chars;
48      ByteArrayOutputStream2 _bytes;
49      
50  
51      /* ------------------------------------------------------------ */
52      public HttpOutput(AbstractGenerator generator, long maxIdleTime)
53      {
54          _generator=generator;
55          _maxIdleTime=maxIdleTime;
56      }
57      
58      /* ------------------------------------------------------------ */
59      /*
60       * @see java.io.OutputStream#close()
61       */
62      @Override
63      public void close() throws IOException
64      {
65          _closed=true;
66      }
67      
68      /* ------------------------------------------------------------ */
69      public void reopen()
70      {
71          _closed=false;
72      }
73      
74      /* ------------------------------------------------------------ */
75      @Override
76      public void flush() throws IOException
77      {
78          _generator.flush(_maxIdleTime);
79      }
80  
81      /* ------------------------------------------------------------ */
82      @Override
83      public void write(byte[] b, int off, int len) throws IOException
84      {
85          _buf.wrap(b, off, len);
86          write(_buf);
87          _buf.wrap(AbstractGenerator.NO_BYTES);
88      }
89  
90      /* ------------------------------------------------------------ */
91      /*
92       * @see java.io.OutputStream#write(byte[])
93       */
94      @Override
95      public void write(byte[] b) throws IOException
96      {
97          _buf.wrap(b);
98          write(_buf);
99          _buf.wrap(AbstractGenerator.NO_BYTES);
100     }
101 
102     /* ------------------------------------------------------------ */
103     /*
104      * @see java.io.OutputStream#write(int)
105      */
106     @Override
107     public void write(int b) throws IOException
108     {
109         if (_closed)
110             throw new IOException("Closed");
111         if (!_generator.isOpen())
112             throw new EofException();
113         
114         // Block until we can add _content.
115         while (_generator.isBufferFull())
116         {
117             _generator.blockForOutput(_maxIdleTime);
118             if (_closed)
119                 throw new IOException("Closed");
120             if (!_generator.isOpen())
121                 throw new EofException();
122         }
123 
124         // Add the _content
125         if (_generator.addContent((byte)b))
126             // Buffers are full so flush.
127             flush();
128        
129         if (_generator.isContentWritten())
130         {
131             flush();
132             close();
133         }
134     }
135 
136     /* ------------------------------------------------------------ */
137     private void write(Buffer buffer) throws IOException
138     {
139         if (_closed)
140             throw new IOException("Closed");
141         if (!_generator.isOpen())
142             throw new EofException();
143         
144         // Block until we can add _content.
145         while (_generator.isBufferFull())
146         {
147             _generator.blockForOutput(_maxIdleTime);
148             if (_closed)
149                 throw new IOException("Closed");
150             if (!_generator.isOpen())
151                 throw new EofException();
152         }
153 
154         // Add the _content
155         _generator.addContent(buffer, Generator.MORE);
156 
157         // Have to flush and complete headers?
158         if (_generator.isBufferFull())
159             flush();
160         
161         if (_generator.isContentWritten())
162         {
163             flush();
164             close();
165         }
166 
167         // Block until our buffer is free
168         while (buffer.length() > 0 && _generator.isOpen())
169             _generator.blockForOutput(_maxIdleTime);
170     }
171 
172     /* ------------------------------------------------------------ */
173     /* 
174      * @see javax.servlet.ServletOutputStream#print(java.lang.String)
175      */
176     @Override
177     public void print(String s) throws IOException
178     {
179         write(s.getBytes());
180     }
181 }