1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2000-2009 Oracle. All rights reserved. 5 * 6 * $Id$ 7 */ 8 9package com.sleepycat.bind.serial; 10 11import com.sleepycat.util.FastOutputStream; 12 13/** 14 * A base class for serial bindings creators that provides control over the 15 * allocation of the output buffer. 16 * 17 * <p>Serial bindings append data to a {@link FastOutputStream} instance. This 18 * object has a byte array buffer that is resized when it is full. The 19 * reallocation of this buffer can be a performance factor for some 20 * applications using large objects. To manage this issue, the {@link 21 * #setSerialBufferSize} method may be used to control the initial size of the 22 * buffer, and the {@link #getSerialOutput} method may be overridden by 23 * subclasses to take over creation of the FastOutputStream object.</p> 24 * 25 * @see <a href="SerialBinding.html#evolution">Class Evolution</a> 26 * 27 * @author Mark Hayes 28 */ 29public class SerialBase { 30 31 private int outputBufferSize; 32 33 /** 34 * Initializes the initial output buffer size to zero. 35 * 36 * <p>Unless {@link #setSerialBufferSize} is called, the default {@link 37 * FastOutputStream#DEFAULT_INIT_SIZE} size will be used.</p> 38 */ 39 public SerialBase() { 40 outputBufferSize = 0; 41 } 42 43 /** 44 * Sets the initial byte size of the output buffer that is allocated by the 45 * default implementation of {@link #getSerialOutput}. 46 * 47 * <p>If this property is zero (the default), the default {@link 48 * FastOutputStream#DEFAULT_INIT_SIZE} size is used.</p> 49 * 50 * @param byteSize the initial byte size of the output buffer, or zero to 51 * use the default size. 52 */ 53 public void setSerialBufferSize(int byteSize) { 54 outputBufferSize = byteSize; 55 } 56 57 /** 58 * Returns the initial byte size of the output buffer. 59 * 60 * @return the initial byte size of the output buffer. 61 * 62 * @see #setSerialBufferSize 63 */ 64 public int getSerialBufferSize() { 65 return outputBufferSize; 66 } 67 68 /** 69 * Returns an empty SerialOutput instance that will be used by the serial 70 * binding or key creator. 71 * 72 * <p>The default implementation of this method creates a new SerialOutput 73 * with an initial buffer size that can be changed using the {@link 74 * #setSerialBufferSize} method.</p> 75 * 76 * <p>This method may be overridden to return a FastOutputStream instance. 77 * For example, an instance per thread could be created and returned by 78 * this method. If a FastOutputStream instance is reused, be sure to call 79 * its {@link FastOutputStream#reset} method before each use.</p> 80 * 81 * @param object is the object to be written to the serial output, and may 82 * be used by subclasses to determine the size of the output buffer. 83 * 84 * @return an empty FastOutputStream instance. 85 * 86 * @see #setSerialBufferSize 87 */ 88 protected FastOutputStream getSerialOutput(Object object) { 89 int byteSize = getSerialBufferSize(); 90 if (byteSize != 0) { 91 return new FastOutputStream(byteSize); 92 } else { 93 return new FastOutputStream(); 94 } 95 } 96} 97