~ubuntu-branches/ubuntu/trusty/ehcache/trusty

« back to all changes in this revision

Viewing changes to net/sf/ehcache/util/MemoryEfficientByteArrayOutputStream.java

  • Committer: Bazaar Package Importer
  • Author(s): Varun Hiremath
  • Date: 2008-07-14 02:08:53 UTC
  • mfrom: (1.1.2 upstream) (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080714020853-37uz6uzh6mc5mcgx
Tags: 1.5.0-1
* New upstream release
* Add libjgroups-java to Build-Depends-Indep
* Bump Standards-Version to 3.8.0
* debian/copyright: remove the full text of Apache 2.0 license, as now
  is included in common licenses

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *  Copyright 2003-2007 Luck Consulting Pty Ltd
 
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$
 
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. Not all may be valid. Use only up to getSize()
 
58
     *
 
59
     * @return the underlying byte[]
 
60
     */
 
61
    public synchronized byte getBytes()[] {
 
62
        return buf;
 
63
    }
 
64
 
 
65
    /**
 
66
     * Factory method
 
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
 
71
     */
 
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);
 
76
        objstr.close();
 
77
        return outstr;
 
78
    }
 
79
 
 
80
    /**
 
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
 
87
     */
 
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);
 
92
        objstr.close();
 
93
        lastSize = outstr.getBytes().length;
 
94
        return outstr;
 
95
    }
 
96
}