View Javadoc

1   /**********************************************************************
2    * ChannelOutputStream.java
3    * created on 16.08.2004 by netseeker
4    * $Source: /cvsroot/ejoe/EJOE/src/de/netseeker/ejoe/io/ByteBufferOutputStream.java,v $
5    * $Date: 2006/02/04 14:15:52 $
6    * $Revision: 1.10 $
7    *********************************************************************/
8   
9   package de.netseeker.ejoe.io;
10  
11  import java.io.IOException;
12  import java.io.OutputStream;
13  import java.nio.ByteBuffer;
14  
15  import de.netseeker.ejoe.EJConstants;
16  
17  /***
18   * Implements an input stream for a NIO ByteBuffer. This class is based on an
19   * article found at the homepage of Michael W. Spille.
20   * 
21   * @author Michael W. Spille
22   * @author netseeker aka Michael Manske
23   * @link http://www.krisnmike.com/mike/nio.html
24   */
25  public class ByteBufferOutputStream extends OutputStream
26  {
27  
28  	private ByteBuffer	buffer;
29  
30  	/***
31  	 * 
32  	 */
33  	public ByteBufferOutputStream()
34  	{
35  		this(EJConstants.NIO_BYTEBUFFER_STREAM_SIZE);
36  	}
37  
38  	/***
39  	 * @param startingSize
40  	 */
41  	public ByteBufferOutputStream(int startingSize)
42  	{
43  		buffer = ByteBuffer.allocateDirect(startingSize);
44  	}
45  
46  	/***
47  	 * @param buffer
48  	 */
49  	public ByteBufferOutputStream(ByteBuffer buffer)
50  	{
51  		this.buffer = buffer;
52  	}
53  
54  	/***
55  	 * Reset the underlying byte buffer back to the starting state
56  	 */
57  	public void reset()
58  	{
59  		buffer.clear();
60  	}
61  
62  	/***
63  	 * @return
64  	 */
65  	public ByteBuffer getBackingBuffer()
66  	{
67  		return (buffer);
68  	}
69  
70  	/*
71  	 * (non-Javadoc)
72  	 * 
73  	 * @see java.io.OutputStream#write(int)
74  	 */
75  	public void write(int b) throws IOException
76  	{
77  		if (!buffer.hasRemaining())
78  		{
79  			realloc(buffer.limit() + EJConstants.NIO_BYTEBUFFER_STREAM_SIZE);
80  		}
81  		buffer.put((byte) b);
82  	}
83  
84  	/*
85  	 * (non-Javadoc)
86  	 * 
87  	 * @see java.io.OutputStream#write(byte[])
88  	 */
89  	public void write(byte[] b) throws IOException
90  	{
91  		if (buffer.remaining() < b.length)
92  		{
93  			realloc(buffer.limit() + (b.length - buffer.remaining()) + EJConstants.NIO_BYTEBUFFER_STREAM_SIZE);
94  		}
95  		buffer.put(b);
96  	}
97  
98  	/*
99  	 * (non-Javadoc)
100 	 * 
101 	 * @see java.io.OutputStream#write(byte[], int, int)
102 	 */
103 	public void write(byte[] b, int off, int len)
104 	{
105 		if (buffer.remaining() < len)
106 		{
107 			realloc(buffer.limit() + (len - buffer.remaining()) + EJConstants.NIO_BYTEBUFFER_STREAM_SIZE);
108 		}
109 		buffer.put(b, off, len);
110 	}
111 
112 	private void realloc(int newSize)
113 	{
114 		ByteBuffer newOutBytes = ByteBuffer.allocateDirect(newSize);
115 		newOutBytes.clear();
116 		buffer.flip();
117 		newOutBytes.put(buffer);
118 		buffer = newOutBytes;
119 	}
120 
121 	/*
122 	 * (non-Javadoc)
123 	 * 
124 	 * @see java.io.OutputStream#flush()
125 	 */
126 	public void flush()
127 	{
128 	}
129 
130 	/*
131 	 * (non-Javadoc)
132 	 * 
133 	 * @see java.io.OutputStream#close()
134 	 */
135 	public void close() throws IOException
136 	{
137 		this.buffer.clear();
138 	}
139 }