changeset 985:8fef34f665e7

remove HttpOutput
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 16 Oct 2016 23:47:23 -0600
parents 7b0fa315e835
children 4f2d04c72781
files src/org/eclipse/jetty/server/AbstractHttpConnection.java src/org/eclipse/jetty/server/HttpOutput.java src/org/eclipse/jetty/server/HttpWriter.java
diffstat 3 files changed, 105 insertions(+), 215 deletions(-) [+]
line wrap: on
line diff
--- a/src/org/eclipse/jetty/server/AbstractHttpConnection.java	Sun Oct 16 23:11:15 2016 -0600
+++ b/src/org/eclipse/jetty/server/AbstractHttpConnection.java	Sun Oct 16 23:47:23 2016 -0600
@@ -21,6 +21,7 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.PrintWriter;
+import java.io.Writer;
 
 import javax.servlet.DispatcherType;
 import javax.servlet.RequestDispatcher;
@@ -46,9 +47,11 @@
 import org.eclipse.jetty.io.Buffer;
 import org.eclipse.jetty.io.BufferCache.CachedBuffer;
 import org.eclipse.jetty.io.Buffers;
+import org.eclipse.jetty.io.ByteArrayBuffer;
 import org.eclipse.jetty.io.EndPoint;
 import org.eclipse.jetty.io.EofException;
 import org.eclipse.jetty.io.RuntimeIOException;
+import org.eclipse.jetty.util.ByteArrayOutputStream2;
 import org.eclipse.jetty.util.QuotedStringTokenizer;
 import org.eclipse.jetty.util.StringUtil;
 import org.eclipse.jetty.util.URIUtil;
@@ -715,12 +718,83 @@
 	}
 
 
-	public final class Output extends HttpOutput
+	public final class Output extends ServletOutputStream
 	{
-		Output()
+		private boolean _closed;
+		private ByteArrayBuffer _onebyte;
+		
+		// These are held here for reuse by Writer
+		String _characterEncoding;
+		Writer _converter;
+		char[] _chars;
+		ByteArrayOutputStream2 _bytes;
+				
+		public final void reopen()
+		{
+			_closed = false;
+		}
+		
+		@Override
+		public final void write(byte[] b, int off, int len) throws IOException
+		{
+			write(new ByteArrayBuffer(b,off,len));
+		}
+	
+		@Override
+		public final void write(byte[] b) throws IOException
+		{
+			write(new ByteArrayBuffer(b));
+		}
+	
+		@Override
+		public final void write(int b) throws IOException
 		{
-			super(AbstractHttpConnection.this);
+			if (_onebyte==null)
+				_onebyte = new ByteArrayBuffer(1);
+			else
+				_onebyte.clear();
+			_onebyte.put((byte)b);
+			write(_onebyte);
 		}
+	
+		private void write(Buffer buffer) throws IOException
+		{
+			if (_closed)
+				throw new IOException("Closed");
+			if (!_generator.isOpen())
+				throw new EofException();
+			
+			// Block until we can add _content.
+			while (_generator.isBufferFull())
+			{
+				_generator.blockForOutput(getMaxIdleTime());
+				if (_closed)
+					throw new IOException("Closed");
+				if (!_generator.isOpen())
+					throw new EofException();
+			}
+	
+			// Add the _content
+			_generator.addContent(buffer, HttpGenerator.MORE);
+	
+			// Have to flush and complete headers?
+			
+			if (_generator.isAllContentWritten())
+			{
+				flush();
+				close();
+			} 
+			else if (_generator.isBufferFull())
+				commitResponse(HttpGenerator.MORE);
+	
+			// Block until our buffer is free
+			while (buffer.length() > 0 && _generator.isOpen())
+			{
+				_generator.blockForOutput(getMaxIdleTime());
+			}
+		}
+
+
 
 		/* ------------------------------------------------------------ */
 		/*
@@ -729,15 +803,15 @@
 		@Override
 		public void close() throws IOException
 		{
-			if (isClosed())
+			if (_closed)
 				return;
 
-			if (!super._generator.isCommitted())
+			if (!_generator.isCommitted())
 				commitResponse(HttpGenerator.LAST);
 			else
 				flushResponse();
 
-			super.close();
+			_closed = true;
 		}
 
 
@@ -748,9 +822,9 @@
 		@Override
 		public void flush() throws IOException
 		{
-			if (!super._generator.isCommitted())
+			if (!_generator.isCommitted())
 				commitResponse(HttpGenerator.MORE);
-			super.flush();
+			_generator.flush(getMaxIdleTime());
 		}
 
 		/* ------------------------------------------------------------ */
@@ -760,25 +834,25 @@
 		@Override
 		public void print(String s) throws IOException
 		{
-			if (isClosed())
+			if (_closed)
 				throw new IOException("Closed");
-			PrintWriter writer=getPrintWriter(null);
+			PrintWriter writer = getPrintWriter(null);
 			writer.print(s);
 		}
 
 		public void sendResponse(Buffer response) throws IOException
 		{
-			super._generator.sendResponse(response);
+			_generator.sendResponse(response);
 		}
 
 		public void sendContent(Object content) throws IOException
 		{
 			Resource resource=null;
 
-			if (isClosed())
+			if (_closed)
 				throw new IOException("Closed");
 
-			if (super._generator.isWritten())
+			if (_generator.isWritten())
 				throw new IllegalStateException("!empty");
 
 			if (content instanceof Resource)
@@ -791,7 +865,7 @@
 			// Process content.
 			if (content instanceof Buffer)
 			{
-				super._generator.addContent((Buffer) content, HttpGenerator.LAST);
+				_generator.addContent((Buffer) content, HttpGenerator.LAST);
 				commitResponse(HttpGenerator.LAST);
 			}
 			else if (content instanceof InputStream)
@@ -800,21 +874,21 @@
 
 				try
 				{
-					int max = super._generator.prepareUncheckedAddContent();
-					Buffer buffer = super._generator.getUncheckedBuffer();
+					int max = _generator.prepareUncheckedAddContent();
+					Buffer buffer = _generator.getUncheckedBuffer();
 
 					int len=buffer.readFrom(in,max);
 
 					while (len>=0)
 					{
-						super._generator.completeUncheckedAddContent();
+						_generator.completeUncheckedAddContent();
 						_out.flush();
 
-						max = super._generator.prepareUncheckedAddContent();
-						buffer = super._generator.getUncheckedBuffer();
+						max = _generator.prepareUncheckedAddContent();
+						buffer = _generator.getUncheckedBuffer();
 						len=buffer.readFrom(in,max);
 					}
-					super._generator.completeUncheckedAddContent();
+					_generator.completeUncheckedAddContent();
 					_out.flush();
 				}
 				finally
--- a/src/org/eclipse/jetty/server/HttpOutput.java	Sun Oct 16 23:11:15 2016 -0600
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,182 +0,0 @@
-//
-//  ========================================================================
-//  Copyright (c) 1995-2014 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.IOException;
-import java.io.Writer;
-
-import javax.servlet.ServletOutputStream;
-
-import org.eclipse.jetty.http.HttpGenerator;
-import org.eclipse.jetty.io.Buffer;
-import org.eclipse.jetty.io.ByteArrayBuffer;
-import org.eclipse.jetty.io.EofException;
-import org.eclipse.jetty.util.ByteArrayOutputStream2;
-
-/** Output.
- * 
- * <p>
- * Implements  {@link javax.servlet.ServletOutputStream} from the <code>javax.servlet</code> package.   
- * </p>
- * A {@link ServletOutputStream} implementation that writes content
- * to a {@link HttpGenerator}.   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 HttpGenerator _generator;
-	private boolean _closed;
-	private ByteArrayBuffer _onebyte;
-	
-	// These are held here for reuse by Writer
-	String _characterEncoding;
-	Writer _converter;
-	char[] _chars;
-	ByteArrayOutputStream2 _bytes;
-
-	/* ------------------------------------------------------------ */
-	public HttpOutput(AbstractHttpConnection connection)
-	{
-		_connection = connection;
-		_generator = connection._generator;
-	}
-
-	/* ------------------------------------------------------------ */
-	public int getMaxIdleTime()
-	{
-		return _connection.getMaxIdleTime();
-	}
-	
-	/* ------------------------------------------------------------ */
-	public boolean isWritten()
-	{
-		return _generator.getContentWritten()>0;
-	}
-	
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see java.io.OutputStream#close()
-	 */
-	@Override
-	public void close() throws IOException
-	{
-		_closed=true;
-	}
-	
-	/* ------------------------------------------------------------ */
-	public boolean isClosed()
-	{
-		return _closed;
-	}
-	
-	/* ------------------------------------------------------------ */
-	public void reopen()
-	{
-		_closed=false;
-	}
-	
-	/* ------------------------------------------------------------ */
-	@Override
-	public void flush() throws IOException
-	{
-		_generator.flush(getMaxIdleTime());
-	}
-
-	/* ------------------------------------------------------------ */
-	@Override
-	public void write(byte[] b, int off, int len) throws IOException
-	{
-		write(new ByteArrayBuffer(b,off,len));
-	}
-
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see java.io.OutputStream#write(byte[])
-	 */
-	@Override
-	public void write(byte[] b) throws IOException
-	{
-		write(new ByteArrayBuffer(b));
-	}
-
-	
-	/* ------------------------------------------------------------ */
-	/*
-	 * @see java.io.OutputStream#write(int)
-	 */
-	@Override
-	public void write(int b) throws IOException
-	{
-		if (_onebyte==null)
-			_onebyte=new ByteArrayBuffer(1);
-		else
-			_onebyte.clear();
-		_onebyte.put((byte)b);
-		write(_onebyte);
-	}
-
-	/* ------------------------------------------------------------ */
-	private void write(Buffer buffer) throws IOException
-	{
-		if (_closed)
-			throw new IOException("Closed");
-		if (!_generator.isOpen())
-			throw new EofException();
-		
-		// Block until we can add _content.
-		while (_generator.isBufferFull())
-		{
-			_generator.blockForOutput(getMaxIdleTime());
-			if (_closed)
-				throw new IOException("Closed");
-			if (!_generator.isOpen())
-				throw new EofException();
-		}
-
-		// Add the _content
-		_generator.addContent(buffer, HttpGenerator.MORE);
-
-		// Have to flush and complete headers?
-		
-		if (_generator.isAllContentWritten())
-		{
-			flush();
-			close();
-		} 
-		else if (_generator.isBufferFull())
-			_connection.commitResponse(HttpGenerator.MORE);
-
-		// Block until our buffer is free
-		while (buffer.length() > 0 && _generator.isOpen())
-		{
-			_generator.blockForOutput(getMaxIdleTime());
-		}
-	}
-
-	/* ------------------------------------------------------------ */
-	/* 
-	 * @see javax.servlet.ServletOutputStream#print(java.lang.String)
-	 */
-	@Override
-	public void print(String s) throws IOException
-	{
-		write(s.getBytes());
-	}
-}
--- a/src/org/eclipse/jetty/server/HttpWriter.java	Sun Oct 16 23:11:15 2016 -0600
+++ b/src/org/eclipse/jetty/server/HttpWriter.java	Sun Oct 16 23:47:23 2016 -0600
@@ -27,7 +27,7 @@
 import org.eclipse.jetty.util.StringUtil;
 
 /** OutputWriter.
- * A writer that can wrap a {@link HttpOutput} stream and provide
+ * A writer that can wrap a {@link AbstractHttpConnection.Output} stream and provide
  * character encodings.
  *
  * The UTF-8 encoding is done by this class and no additional 
@@ -42,11 +42,11 @@
 	private static final int WRITE_ISO1 = 1;
 	private static final int WRITE_UTF8 = 2;
 	
-	private final HttpOutput _out;
+	private final AbstractHttpConnection.Output _out;
 	private int _writeMode;
 	private int _surrogate;
 
-	public HttpWriter(HttpOutput out)
+	public HttpWriter(AbstractHttpConnection.Output out)
 	{
 		_out = out;
 		_surrogate = 0; // AS lastUTF16CodePoint
@@ -108,11 +108,9 @@
 	@Override
 	public void write (char[] s,int offset, int length) throws IOException
 	{              
-		HttpOutput out = _out; 
-		
 		while (length > 0)
 		{  
-			out._bytes.reset();
+			_out._bytes.reset();
 			int chars = length>MAX_OUTPUT_CHARS?MAX_OUTPUT_CHARS:length;
 			
 			switch (_writeMode)
@@ -127,8 +125,8 @@
 
 				case WRITE_ISO1:
 				{
-					byte[] buffer=out._bytes.getBuf();
-					int bytes=out._bytes.getCount();
+					byte[] buffer = _out._bytes.getBuf();
+					int bytes = _out._bytes.getCount();
 					
 					if (chars>buffer.length-bytes)
 						chars=buffer.length-bytes;
@@ -139,15 +137,15 @@
 						buffer[bytes++]=(byte)(c<256?c:'?'); // ISO-1 and UTF-8 match for 0 - 255
 					}
 					if (bytes>=0)
-						out._bytes.setCount(bytes);
+						_out._bytes.setCount(bytes);
 
 					break;
 				}
 
 				case WRITE_UTF8:
 				{
-					byte[] buffer=out._bytes.getBuf();
-					int bytes=out._bytes.getCount();
+					byte[] buffer = _out._bytes.getBuf();
+					int bytes = _out._bytes.getCount();
 
 					if (bytes+chars>buffer.length)
 						chars=buffer.length-bytes;
@@ -270,14 +268,14 @@
 							}
 						}
 					}
-					out._bytes.setCount(bytes);
+					_out._bytes.setCount(bytes);
 					break;
 				}
 				default:
 					throw new IllegalStateException();
 			}
 			
-			out._bytes.writeTo(out);
+			_out._bytes.writeTo(_out);
 			length-=chars;
 			offset+=chars;
 		}