2
* Copyright 2003-2007 Luck Consulting Pty Ltd
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package net.sf.ehcache.util;
19
import java.io.ByteArrayOutputStream;
20
import java.io.IOException;
21
import java.io.ObjectOutputStream;
22
import java.io.Serializable;
25
* This class is designed to minimise the number of System.arraycopy(); methods
26
* required to complete.
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.
31
* @author <a href="mailto:gluck@gregluck.com">Greg Luck</a>
34
public final class MemoryEfficientByteArrayOutputStream extends ByteArrayOutputStream {
37
* byte[] payloads are not expected to be tiny.
39
private static final int BEST_GUESS_SIZE = 512;
41
private static int lastSize = BEST_GUESS_SIZE;
44
* Creates a new byte array output stream, with a buffer capacity of
45
* the specified size, in bytes.
47
* @param size the initial size.
49
public MemoryEfficientByteArrayOutputStream(int size) {
57
* Gets the bytes. Not all may be valid. Use only up to getSize()
59
* @return the underlying byte[]
61
public synchronized byte getBytes()[] {
67
* @param serializable any Object that implements Serializable
68
* @param estimatedPayloadSize how many bytes is expected to be in the Serialized representation
69
* @return a ByteArrayOutputStream with a Serialized object in it
70
* @throws java.io.IOException if something goes wrong with the Serialization
72
public static MemoryEfficientByteArrayOutputStream serialize(Serializable serializable, int estimatedPayloadSize) throws IOException {
73
MemoryEfficientByteArrayOutputStream outstr = new MemoryEfficientByteArrayOutputStream(estimatedPayloadSize);
74
ObjectOutputStream objstr = new ObjectOutputStream(outstr);
75
objstr.writeObject(serializable);
81
* Factory method. This method optimises memory by trying to make a better guess than the Java default
82
* of 32 bytes by assuming the starting point for the serialized size will be what it was last time
83
* this method was called.
84
* @param serializable any Object that implements Serializable
85
* @return a ByteArrayOutputStream with a Serialized object in it
86
* @throws java.io.IOException if something goes wrong with the Serialization
88
public static MemoryEfficientByteArrayOutputStream serialize(Serializable serializable) throws IOException {
89
MemoryEfficientByteArrayOutputStream outstr = new MemoryEfficientByteArrayOutputStream(lastSize);
90
ObjectOutputStream objstr = new ObjectOutputStream(outstr);
91
objstr.writeObject(serializable);
93
lastSize = outstr.getBytes().length;