01: /*
02: * BufferAllocator.java
03: *
04: * Created on November 22, 2006, 1:55 PM
05: *
06: * To change this template, choose Tools | Template Manager
07: * and open the template in the editor.
08: */
09:
10: package com.sun.xml.stream.util;
11:
12: import java.lang.ref.*;
13:
14: public class BufferAllocator {
15: public static int SMALL_SIZE_LIMIT = 128;
16: public static int MEDIUM_SIZE_LIMIT = 2048;
17: public static int LARGE_SIZE_LIMIT = 8192;
18:
19: char[] smallCharBuffer;
20: char[] mediumCharBuffer;
21: char[] largeCharBuffer;
22:
23: byte[] smallByteBuffer;
24: byte[] mediumByteBuffer;
25: byte[] largeByteBuffer;
26:
27: public BufferAllocator() {
28: }
29:
30: public char[] getCharBuffer(int size) {
31: if (size <= SMALL_SIZE_LIMIT) {
32: char[] buffer = smallCharBuffer;
33: smallCharBuffer = null;
34: return buffer;
35: } else if (size <= MEDIUM_SIZE_LIMIT) {
36: char[] buffer = mediumCharBuffer;
37: mediumCharBuffer = null;
38: return buffer;
39: } else if (size <= LARGE_SIZE_LIMIT) {
40: char[] buffer = largeCharBuffer;
41: largeCharBuffer = null;
42: return buffer;
43: }
44: return null;
45: }
46:
47: public void returnCharBuffer(char[] c) {
48: if (c == null) {
49: return;
50: }
51: if (c.length <= SMALL_SIZE_LIMIT) {
52: smallCharBuffer = c;
53: } else if (c.length <= MEDIUM_SIZE_LIMIT) {
54: mediumCharBuffer = c;
55: } else if (c.length <= LARGE_SIZE_LIMIT) {
56: largeCharBuffer = c;
57: }
58: }
59:
60: public byte[] getByteBuffer(int size) {
61: if (size <= SMALL_SIZE_LIMIT) {
62: byte[] buffer = smallByteBuffer;
63: smallByteBuffer = null;
64: return buffer;
65: } else if (size <= MEDIUM_SIZE_LIMIT) {
66: byte[] buffer = mediumByteBuffer;
67: mediumByteBuffer = null;
68: return buffer;
69: } else if (size <= LARGE_SIZE_LIMIT) {
70: byte[] buffer = largeByteBuffer;
71: largeByteBuffer = null;
72: return buffer;
73: }
74: return null;
75: }
76:
77: public void returnByteBuffer(byte[] b) {
78: if (b == null) {
79: return;
80: }
81: if (b.length <= SMALL_SIZE_LIMIT) {
82: smallByteBuffer = b;
83: } else if (b.length <= MEDIUM_SIZE_LIMIT) {
84: mediumByteBuffer = b;
85: } else if (b.length <= LARGE_SIZE_LIMIT) {
86: largeByteBuffer = b;
87: }
88: }
89:
90: }
|