Debian Codegraph @

java/org/doit/io/ByteArray.java

raw symbols SCIP
1 /* $Id: ByteArray.java,v 1.2 1998/08/13 14:33:58 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1996-98 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of Muffin.
7 *
8 * Muffin is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * Muffin is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Muffin; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23 package org.doit.io;
24
25 /**
26 * Class used to represent an array of bytes as an Object.
27 *
28 * @author Mark Boyns
29 */
30 public class ByteArray
31 {
32 public byte bytes[];
33 public int offset = 0;
34
35 /**
36 * Create a ByteArray with the default size.
37 */
38 public ByteArray ()
39 {
40 this (512);
41 }
42
43 /**
44 * Create a ByteArray with a specific default size.
45 */
46 public ByteArray (int size)
47 {
48 bytes = new byte[size];
49 }
50
51 /**
52 * Create a ByteArray from a String.
53 */
54 public ByteArray (String s)
55 {
56 this (s.length ());
57 append (s);
58 }
59
60 /**
61 * Create a ByteArray from an array of bytes.
62 */
63 public ByteArray (byte b[])
64 {
65 this (b.length);
66 append (b);
67 }
68
69 /**
70 * Append a byte.
71 */
72 public void append (byte ch)
73 {
74 if (offset == bytes.length)
75 {
76 byte tmpbytes[] = bytes;
77 bytes = new byte[tmpbytes.length * 2];
78 System.arraycopy (tmpbytes, 0, bytes, 0, offset);
79 }
80 bytes[offset++] = ch;
81 }
82
83 /**
84 * Append a ByteArray.
85 */
86 public void append (ByteArray b)
87 {
88 if (bytes.length - offset < b.length ())
89 {
90 byte tmpbytes[] = bytes;
91 bytes = new byte[tmpbytes.length + b.length ()];
92 System.arraycopy (tmpbytes, 0, bytes, 0, offset);
93 }
94 System.arraycopy (b.bytes, 0, bytes, offset, b.length ());
95 offset += b.length ();
96 }
97
98 /**
99 * Append an array of bytes.
100 */
101 public void append (byte b[])
102 {
103 if (bytes.length - offset < b.length)
104 {
105 byte tmpbytes[] = bytes;
106 bytes = new byte[tmpbytes.length + b.length];
107 System.arraycopy (tmpbytes, 0, bytes, 0, offset);
108 }
109 System.arraycopy (b, 0, bytes, offset, b.length);
110 offset += b.length;
111 }
112
113 /**
114 * Append a String.
115 */
116 public void append (String s)
117 {
118 append (s.getBytes ());
119 }
120
121 /**
122 * Convert to String.
123 */
124 public String toString ()
125 {
126 return new String (bytes, 0, offset);
127 }
128
129 /**
130 * Return the bytes.
131 */
132 public byte getBytes () []
133 {
134 return bytes;
135 }
136
137 public byte get (int i)
138 {
139 return bytes[i];
140 }
141
142 /**
143 * Return the number of bytes.
144 */
145 public int length ()
146 {
147 return offset;
148 }
149
150 public void erase ()
151 {
152 offset = 0;
153 }
154
155 public void chop ()
156 {
157 chop (1);
158 }
159
160 public void chop (int i)
161 {
162 offset -= i;
163 if (offset < 0)
164 {
165 offset = 0;
166 }
167 }
168
169 public static void main (String args[])
170 {
171 ByteArray b = new ByteArray (3);
172 b.append ("foo");
173 b.append ("bar");
174 b.append ("joe");
175 System.out.println (b.toString ());
176
177 ByteArray tmp = new ByteArray (1);
178 tmp.append ("test");
179 b.append (tmp);
180 System.out.println (b.toString ());
181 }
182 }