File: ByteArray.java

package info (click to toggle)
rplay 3.3.2-12
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,200 kB
  • ctags: 3,492
  • sloc: ansic: 42,124; sh: 2,883; makefile: 1,234; perl: 742; tcl: 345; java: 220
file content (182 lines) | stat: -rw-r--r-- 3,650 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* $Id: ByteArray.java,v 1.2 1998/08/13 14:33:58 boyns Exp $ */

/*
 * Copyright (C) 1996-98 Mark R. Boyns <boyns@doit.org>
 *
 * This file is part of Muffin.
 *
 * Muffin is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Muffin is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Muffin; see the file COPYING.  If not, write to the
 * Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */
package org.doit.io;

/**
 * Class used to represent an array of bytes as an Object.
 *
 * @author Mark Boyns
 */
public class ByteArray
{
    public byte bytes[];
    public int offset = 0;

    /**
     * Create a ByteArray with the default size.
     */
    public ByteArray ()
    {
	this (512);
    }

    /**
     * Create a ByteArray with a specific default size.
     */
    public ByteArray (int size)
    {
	bytes = new byte[size];
    }

    /**
     * Create a ByteArray from a String.
     */
    public ByteArray (String s)
    {
	this (s.length ());
	append (s);
    }

    /**
     * Create a ByteArray from an array of bytes.
     */
    public ByteArray (byte b[])
    {
	this (b.length);
	append (b);
    }
    
    /**
     * Append a byte.
     */
    public void append (byte ch)
    {
        if (offset == bytes.length)
        {
            byte tmpbytes[] = bytes;
            bytes = new byte[tmpbytes.length * 2];
            System.arraycopy (tmpbytes, 0, bytes, 0, offset);
        }
        bytes[offset++] = ch;
    }

    /**
     * Append a ByteArray.
     */
    public void append (ByteArray b)
    {
	if (bytes.length - offset < b.length ())
	{
	    byte tmpbytes[] = bytes;
	    bytes = new byte[tmpbytes.length + b.length ()];
	    System.arraycopy (tmpbytes, 0, bytes, 0, offset);
	}
	System.arraycopy (b.bytes, 0, bytes, offset, b.length ());
	offset += b.length ();
    }

    /**
     * Append an array of bytes.
     */
    public void append (byte b[])
    {
	if (bytes.length - offset < b.length)
	{
	    byte tmpbytes[] = bytes;
	    bytes = new byte[tmpbytes.length + b.length];
	    System.arraycopy (tmpbytes, 0, bytes, 0, offset);
	}
	System.arraycopy (b, 0, bytes, offset, b.length);
	offset += b.length;
    }

    /**
     * Append a String.
     */
    public void append (String s)
    {
	append (s.getBytes ());
    }

    /**
     * Convert to String.
     */
    public String toString ()
    {
	return new String (bytes, 0, offset);
    }

    /**
     * Return the bytes.
     */
    public byte getBytes () []
    {
	return bytes;
    }

    public byte get (int i)
    {
	return bytes[i];
    }

    /**
     * Return the number of bytes.
     */
    public int length ()
    {
	return offset;
    }

    public void erase ()
    {
	offset = 0;
    }
    
    public void chop ()
    {
	chop (1);
    }

    public void chop (int i)
    {
	offset -= i;
	if (offset < 0)
	{
	    offset = 0;
	}
    }

    public static void main (String args[])
    {
	ByteArray b = new ByteArray (3);
	b.append ("foo");
	b.append ("bar");
	b.append ("joe");
	System.out.println (b.toString ());
	
	ByteArray tmp = new ByteArray (1);
	tmp.append ("test");
	b.append (tmp);
	System.out.println (b.toString ());
    }
}