View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.util;
18  
19  import java.io.ByteArrayOutputStream;
20  import java.io.IOException;
21  import java.io.ObjectOutputStream;
22  import java.io.Serializable;
23  
24  /***
25   * This class is designed to minimise the number of System.arraycopy(); methods
26   * required to complete.
27   *
28   * ByteArrayOutputStream in the JDK is tuned for a wide variety of purposes. This sub-class
29   * starts with an initial size which is a closer match for ehcache usage.
30   * 
31   * @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
32   * @version $Id: MemoryEfficientByteArrayOutputStream.html 13146 2011-08-01 17:12:39Z oletizi $
33   */
34  public final class MemoryEfficientByteArrayOutputStream extends ByteArrayOutputStream {
35  
36      /***
37       * byte[] payloads are not expected to be tiny.
38       */
39      private static final int BEST_GUESS_SIZE = 512;
40  
41      private static int lastSize = BEST_GUESS_SIZE;
42  
43      /***
44       * Creates a new byte array output stream, with a buffer capacity of
45       * the specified size, in bytes.
46       *
47       * @param size the initial size.
48       */
49      public MemoryEfficientByteArrayOutputStream(int size) {
50          super(size);
51      }
52  
53  
54  
55  
56      /***
57       * Gets the bytes.
58       *
59       * @return the underlying byte[], or a copy if the byte[] is oversized
60       */
61      public synchronized byte getBytes()[] {
62          if (buf.length == size()) {
63              return buf;
64          } else {
65              byte[] copy = new byte[size()];
66              System.arraycopy(buf, 0, copy, 0, size());
67              return copy;
68          }
69      }
70  
71      /***
72       * Factory method
73       * @param serializable any Object that implements Serializable
74       * @param estimatedPayloadSize how many bytes is expected to be in the Serialized representation
75       * @return a ByteArrayOutputStream with a Serialized object in it
76       * @throws java.io.IOException if something goes wrong with the Serialization
77       */
78      public static MemoryEfficientByteArrayOutputStream serialize(Serializable serializable, int estimatedPayloadSize) throws IOException {
79          MemoryEfficientByteArrayOutputStream outstr = new MemoryEfficientByteArrayOutputStream(estimatedPayloadSize);
80          ObjectOutputStream objstr = new ObjectOutputStream(outstr);
81          objstr.writeObject(serializable);
82          objstr.close();
83          return outstr;
84      }
85  
86      /***
87       * Factory method. This method optimises memory by trying to make a better guess than the Java default
88       * of 32 bytes by assuming the starting point for the serialized size will be what it was last time
89       * this method was called.
90       * @param serializable any Object that implements Serializable
91       * @return a ByteArrayOutputStream with a Serialized object in it
92       * @throws java.io.IOException if something goes wrong with the Serialization
93       */
94      public static MemoryEfficientByteArrayOutputStream serialize(Serializable serializable) throws IOException {
95          MemoryEfficientByteArrayOutputStream outstr = new MemoryEfficientByteArrayOutputStream(lastSize);
96          ObjectOutputStream objstr = new ObjectOutputStream(outstr);
97          objstr.writeObject(serializable);
98          objstr.close();
99          lastSize = outstr.getBytes().length;
100         return outstr;
101     }
102 }