1 /* 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package com.sun.xml.internal.stream.util; 27 28 import java.lang.ref.*; 29 30 /** 31 * Buffer allocator for buffers of sizes 128 B, 2 KB and 8 KB. Includes 32 * methods for allocating and freeing buffers. 33 * 34 * @author Binu.John@sun.com 35 * @author Santiago.PericasGeertsen@sun.com 36 */ 37 public class BufferAllocator { 38 private static final int SMALL_SIZE_LIMIT = 128; 39 private static final int MEDIUM_SIZE_LIMIT = 2048; 40 private static final int LARGE_SIZE_LIMIT = 8192; 41 42 char[] smallCharBuffer; 43 char[] mediumCharBuffer; 44 char[] largeCharBuffer; 45 46 byte[] smallByteBuffer; 47 byte[] mediumByteBuffer; 48 byte[] largeByteBuffer; 49 50 public BufferAllocator() { 51 } 52 53 public char[] getCharBuffer(int size) { 54 if (size <= SMALL_SIZE_LIMIT) { 55 char[] buffer = smallCharBuffer; 56 smallCharBuffer = null; 57 return buffer; 58 } 59 else if (size <= MEDIUM_SIZE_LIMIT) { 60 char[] buffer = mediumCharBuffer; 61 mediumCharBuffer = null; 62 return buffer; 63 } 64 else if (size <= LARGE_SIZE_LIMIT) { 65 char[] buffer = largeCharBuffer; 66 largeCharBuffer = null; 67 return buffer; 68 } 69 return null; 70 } 71 72 public void returnCharBuffer(char[] c) { 73 if (c == null) { 74 return; 75 } 76 if (c.length <= SMALL_SIZE_LIMIT) { 77 smallCharBuffer = c; 78 } 79 else if (c.length <= MEDIUM_SIZE_LIMIT) { 80 mediumCharBuffer = c; 81 } 82 else if (c.length <= LARGE_SIZE_LIMIT) { 83 largeCharBuffer = c; 84 } 85 } 86 87 public byte[] getByteBuffer(int size) { 88 if (size <= SMALL_SIZE_LIMIT) { 89 byte[] buffer = smallByteBuffer; 90 smallByteBuffer = null; 91 return buffer; 92 } 93 else if (size <= MEDIUM_SIZE_LIMIT) { 94 byte[] buffer = mediumByteBuffer; 95 mediumByteBuffer = null; 96 return buffer; 97 } 98 else if (size <= LARGE_SIZE_LIMIT) { 99 byte[] buffer = largeByteBuffer; 100 largeByteBuffer = null; 101 return buffer; 102 } 103 return null; 104 } 105 106 public void returnByteBuffer(byte[] b) { 107 if (b == null) { 108 return; 109 } 110 if (b.length <= SMALL_SIZE_LIMIT) { 111 smallByteBuffer = b; 112 } 113 else if (b.length <= MEDIUM_SIZE_LIMIT) { 114 mediumByteBuffer = b; 115 } 116 else if (b.length <= LARGE_SIZE_LIMIT) { 117 largeByteBuffer = b; 118 } 119 } 120 121 }