KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > matuschek > util > ByteBuffer


1 package net.matuschek.util;
2
3 /*********************************************
4     Copyright (c) 2001 by Daniel Matuschek
5 *********************************************/

6
7 /**
8  * A ByteBuffer implements a growable byte array. You can simple
9  * add bytes like you do it using a Vector, but internally the buffer
10  * is implemented as a real array of bytes. This increases memory usage.
11  *
12  * @author Daniel Matuschek
13  * @version $Id $
14  */

15 public class ByteBuffer {
16   
17   protected final int INITIALSIZE=1024;
18   
19   protected int used = 0;
20   protected int size = 0;
21   protected byte[] buff =null;
22   
23   /**
24    * Initializes a new ByteBuffer object and creates
25    * a temporary buffer array of a predefined initial size.
26    * If you want to set your own initial size, use the <code>setSize</code>
27    * method after initializing the object.
28    *
29    */

30   public ByteBuffer() {
31     size=INITIALSIZE;
32     buff=new byte[INITIALSIZE];
33   }
34
35
36   /**
37    * Appends a byte to the end of the buffer
38    *
39    * If the currently reserved memory is used, the size of the
40    * internal buffer will be doubled.
41    * In this case the memory usage will temprary increase by factor 3
42    * because it need a temporary storage for the old data.
43    *
44    * Be sure that you have enough heap memory !
45    *
46    * @param b byte to append
47    */

48   public void append(byte b) {
49     if (used >= size) {
50       doubleBuffer();
51     }
52     
53     buff[used]=b;
54     used++;
55   }
56
57   /**
58    * @return the number of bytes stored in the buffer
59    */

60   public int length() {
61     return used;
62   }
63
64
65   /**
66    * @return the buffer contents as a byte array
67    */

68   public byte[] getContent() {
69     byte[] b = new byte[used];
70     for (int i=0; i<used; i++) {
71       b[i]=buff[i];
72     }
73     return b;
74   }
75
76   /**
77    * removes all contents in the buffer
78    */

79   public void clean() {
80     used=0;
81   }
82
83
84   /**
85    * Sets the size of the internal buffer to
86    * the given value. This is useful, if the size of the
87    * data that should be stored is known.
88    * @param size size of the buffer in Bytes
89    */

90   public void setSize(int size) {
91
92     // if we have already used more data, ignore it !
93
if (size < used) {
94       return;
95     }
96
97     this.size=size;
98
99     // create a new (larger) array
100
byte[] newBuff = new byte[size];
101     
102     // copy contents
103
for (int i=0; i<used; i++) {
104       newBuff[i]=buff[i];
105     }
106
107     buff=newBuff;
108   }
109
110
111   /**
112    * Print the buffer content as a String (use it for debugging only !)
113    * @return a String containing every byte in the buffer as a character
114    */

115   public String JavaDoc toString() {
116     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(buff.length);
117     for (int i=0; i<used; i++) {
118       sb.append(buff[i]);
119     }
120     return sb.toString();
121   }
122
123
124   /**
125    * doubles the size of the internal buffer
126    */

127   protected void doubleBuffer() {
128    // increase size
129
setSize(size*2);
130   }
131
132
133
134 }
135
Popular Tags