PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/core/fr.opensagres.xdocreport.core/src/main/java/fr/opensagres/xdocreport/core/io/internal/ByteArrayOutputStream.java

https://code.google.com/
Java | 360 lines | 182 code | 26 blank | 152 comment | 28 complexity | 26ffffdbd45c620d55e226c629135e73 MD5 | raw file
  1. /**
  2. * Copyright (C) 2011 Angelo Zerr <angelo.zerr@gmail.com> and Pascal Leclercq <pascal.leclercq@gmail.com>
  3. *
  4. * All rights reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. package fr.opensagres.xdocreport.core.io.internal;
  26. /*
  27. * Licensed to the Apache Software Foundation (ASF) under one or more
  28. * contributor license agreements. See the NOTICE file distributed with
  29. * this work for additional information regarding copyright ownership.
  30. * The ASF licenses this file to You under the Apache License, Version 2.0
  31. * (the "License"); you may not use this file except in compliance with
  32. * the License. You may obtain a copy of the License at
  33. *
  34. * http://www.apache.org/licenses/LICENSE-2.0
  35. *
  36. * Unless required by applicable law or agreed to in writing, software
  37. * distributed under the License is distributed on an "AS IS" BASIS,
  38. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  39. * See the License for the specific language governing permissions and
  40. * limitations under the License.
  41. */
  42. import java.io.IOException;
  43. import java.io.InputStream;
  44. import java.io.OutputStream;
  45. import java.io.UnsupportedEncodingException;
  46. import java.util.ArrayList;
  47. import java.util.List;
  48. /**
  49. * This class implements an output stream in which the data is written into a byte array. The buffer automatically grows
  50. * as data is written to it.
  51. * <p>
  52. * The data can be retrieved using <code>toByteArray()</code> and <code>toString()</code>.
  53. * <p>
  54. * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in this class can be called after the stream has
  55. * been closed without generating an <tt>IOException</tt>.
  56. * <p>
  57. * This is an alternative implementation of the java.io.ByteArrayOutputStream class. The original implementation only
  58. * allocates 32 bytes at the beginning. As this class is designed for heavy duty it starts at 1024 bytes. In contrast to
  59. * the original it doesn't reallocate the whole memory block but allocates additional buffers. This way no buffers need
  60. * to be garbage collected and the contents don't have to be copied to the new buffer. This class is designed to behave
  61. * exactly like the original. The only exception is the deprecated toString(int) method that has been ignored.
  62. *
  63. * @author <a href="mailto:jeremias@apache.org">Jeremias Maerki</a>
  64. * @author Holger Hoffstatte
  65. * @version $Id: ByteArrayOutputStream.java 610010 2008-01-08 14:50:59Z niallp $
  66. */
  67. public class ByteArrayOutputStream
  68. extends OutputStream
  69. {
  70. /** A singleton empty byte array. */
  71. private static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
  72. /** The list of buffers, which grows and never reduces. */
  73. private List<byte[]> buffers = new ArrayList<byte[]>();
  74. /** The index of the current buffer. */
  75. private int currentBufferIndex;
  76. /** The total count of bytes in all the filled buffers. */
  77. private int filledBufferSum;
  78. /** The current buffer. */
  79. private byte[] currentBuffer;
  80. /** The total count of bytes written. */
  81. private int count;
  82. /**
  83. * Creates a new byte array output stream. The buffer capacity is initially 1024 bytes, though its size increases if
  84. * necessary.
  85. */
  86. public ByteArrayOutputStream()
  87. {
  88. this( 1024 );
  89. }
  90. /**
  91. * Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
  92. *
  93. * @param size the initial size
  94. * @throws IllegalArgumentException if size is negative
  95. */
  96. public ByteArrayOutputStream( int size )
  97. {
  98. if ( size < 0 )
  99. {
  100. throw new IllegalArgumentException( "Negative initial size: " + size );
  101. }
  102. needNewBuffer( size );
  103. }
  104. /**
  105. * Return the appropriate <code>byte[]</code> buffer specified by index.
  106. *
  107. * @param index the index of the buffer required
  108. * @return the buffer
  109. */
  110. private byte[] getBuffer( int index )
  111. {
  112. return (byte[]) buffers.get( index );
  113. }
  114. /**
  115. * Makes a new buffer available either by allocating a new one or re-cycling an existing one.
  116. *
  117. * @param newcount the size of the buffer if one is created
  118. */
  119. private void needNewBuffer( int newcount )
  120. {
  121. if ( currentBufferIndex < buffers.size() - 1 )
  122. {
  123. // Recycling old buffer
  124. filledBufferSum += currentBuffer.length;
  125. currentBufferIndex++;
  126. currentBuffer = getBuffer( currentBufferIndex );
  127. }
  128. else
  129. {
  130. // Creating new buffer
  131. int newBufferSize;
  132. if ( currentBuffer == null )
  133. {
  134. newBufferSize = newcount;
  135. filledBufferSum = 0;
  136. }
  137. else
  138. {
  139. newBufferSize = Math.max( currentBuffer.length << 1, newcount - filledBufferSum );
  140. filledBufferSum += currentBuffer.length;
  141. }
  142. currentBufferIndex++;
  143. currentBuffer = new byte[newBufferSize];
  144. buffers.add( currentBuffer );
  145. }
  146. }
  147. /**
  148. * Write the bytes to byte array.
  149. *
  150. * @param b the bytes to write
  151. * @param off The start offset
  152. * @param len The number of bytes to write
  153. */
  154. public void write( byte[] b, int off, int len )
  155. {
  156. if ( ( off < 0 ) || ( off > b.length ) || ( len < 0 ) || ( ( off + len ) > b.length ) || ( ( off + len ) < 0 ) )
  157. {
  158. throw new IndexOutOfBoundsException();
  159. }
  160. else if ( len == 0 )
  161. {
  162. return;
  163. }
  164. synchronized ( this )
  165. {
  166. int newcount = count + len;
  167. int remaining = len;
  168. int inBufferPos = count - filledBufferSum;
  169. while ( remaining > 0 )
  170. {
  171. int part = Math.min( remaining, currentBuffer.length - inBufferPos );
  172. System.arraycopy( b, off + len - remaining, currentBuffer, inBufferPos, part );
  173. remaining -= part;
  174. if ( remaining > 0 )
  175. {
  176. needNewBuffer( newcount );
  177. inBufferPos = 0;
  178. }
  179. }
  180. count = newcount;
  181. }
  182. }
  183. /**
  184. * Write a byte to byte array.
  185. *
  186. * @param b the byte to write
  187. */
  188. public synchronized void write( int b )
  189. {
  190. int inBufferPos = count - filledBufferSum;
  191. if ( inBufferPos == currentBuffer.length )
  192. {
  193. needNewBuffer( count + 1 );
  194. inBufferPos = 0;
  195. }
  196. currentBuffer[inBufferPos] = (byte) b;
  197. count++;
  198. }
  199. /**
  200. * Writes the entire contents of the specified input stream to this byte stream. Bytes from the input stream are
  201. * read directly into the internal buffers of this streams.
  202. *
  203. * @param in the input stream to read from
  204. * @return total number of bytes read from the input stream (and written to this stream)
  205. * @throws IOException if an I/O error occurs while reading the input stream
  206. * @since Commons IO 1.4
  207. */
  208. public synchronized int write( InputStream in )
  209. throws IOException
  210. {
  211. int readCount = 0;
  212. int inBufferPos = count - filledBufferSum;
  213. int n = in.read( currentBuffer, inBufferPos, currentBuffer.length - inBufferPos );
  214. while ( n != -1 )
  215. {
  216. readCount += n;
  217. inBufferPos += n;
  218. count += n;
  219. if ( inBufferPos == currentBuffer.length )
  220. {
  221. needNewBuffer( currentBuffer.length );
  222. inBufferPos = 0;
  223. }
  224. n = in.read( currentBuffer, inBufferPos, currentBuffer.length - inBufferPos );
  225. }
  226. return readCount;
  227. }
  228. /**
  229. * Return the current size of the byte array.
  230. *
  231. * @return the current size of the byte array
  232. */
  233. public synchronized int size()
  234. {
  235. return count;
  236. }
  237. /**
  238. * Closing a <tt>ByteArrayOutputStream</tt> has no effect. The methods in this class can be called after the stream
  239. * has been closed without generating an <tt>IOException</tt>.
  240. *
  241. * @throws IOException never (this method should not declare this exception but it has to now due to backwards
  242. * compatability)
  243. */
  244. public void close()
  245. throws IOException
  246. {
  247. // nop
  248. }
  249. /**
  250. * @see java.io.ByteArrayOutputStream#reset()
  251. */
  252. public synchronized void reset()
  253. {
  254. count = 0;
  255. filledBufferSum = 0;
  256. currentBufferIndex = 0;
  257. currentBuffer = getBuffer( currentBufferIndex );
  258. }
  259. /**
  260. * Writes the entire contents of this byte stream to the specified output stream.
  261. *
  262. * @param out the output stream to write to
  263. * @throws IOException if an I/O error occurs, such as if the stream is closed
  264. * @see java.io.ByteArrayOutputStream#writeTo(OutputStream)
  265. */
  266. public synchronized void writeTo( OutputStream out )
  267. throws IOException
  268. {
  269. int remaining = count;
  270. for ( int i = 0; i < buffers.size(); i++ )
  271. {
  272. byte[] buf = getBuffer( i );
  273. int c = Math.min( buf.length, remaining );
  274. out.write( buf, 0, c );
  275. remaining -= c;
  276. if ( remaining == 0 )
  277. {
  278. break;
  279. }
  280. }
  281. }
  282. /**
  283. * Gets the curent contents of this byte stream as a byte array. The result is independent of this stream.
  284. *
  285. * @return the current contents of this output stream, as a byte array
  286. * @see java.io.ByteArrayOutputStream#toByteArray()
  287. */
  288. public synchronized byte[] toByteArray()
  289. {
  290. int remaining = count;
  291. if ( remaining == 0 )
  292. {
  293. return EMPTY_BYTE_ARRAY;
  294. }
  295. byte newbuf[] = new byte[remaining];
  296. int pos = 0;
  297. for ( int i = 0; i < buffers.size(); i++ )
  298. {
  299. byte[] buf = getBuffer( i );
  300. int c = Math.min( buf.length, remaining );
  301. System.arraycopy( buf, 0, newbuf, pos, c );
  302. pos += c;
  303. remaining -= c;
  304. if ( remaining == 0 )
  305. {
  306. break;
  307. }
  308. }
  309. return newbuf;
  310. }
  311. /**
  312. * Gets the curent contents of this byte stream as a string.
  313. *
  314. * @return the contents of the byte array as a String
  315. * @see java.io.ByteArrayOutputStream#toString()
  316. */
  317. public String toString()
  318. {
  319. return new String( toByteArray() );
  320. }
  321. /**
  322. * Gets the curent contents of this byte stream as a string using the specified encoding.
  323. *
  324. * @param enc the name of the character encoding
  325. * @return the string converted from the byte array
  326. * @throws UnsupportedEncodingException if the encoding is not supported
  327. * @see java.io.ByteArrayOutputStream#toString(String)
  328. */
  329. public String toString( String enc )
  330. throws UnsupportedEncodingException
  331. {
  332. return new String( toByteArray(), enc );
  333. }
  334. }