01: /*
02: Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
03:
04: This program is free software; you can redistribute it and/or
05: modify it under the terms of the GNU General Public License
06: as published by the Free Software Foundation; either version 2
07: of the License, or (at your option) any later version.
08:
09: This program is distributed in the hope that it will be useful,
10: but WITHOUT ANY WARRANTY; without even the implied warranty of
11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12: GNU General Public License for more details.
13:
14: You should have received a copy of the GNU General Public License
15: along with this program; if not, write to the Free Software
16: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17: */
18:
19: package org.homedns.dade.jcgrid.util;
20:
21: import java.io.*;
22: import java.util.*;
23:
24: public class ByteVector extends Object {
25: public static byte[] readAll(InputStream is) throws Exception {
26: ByteVector bv = new ByteVector();
27: byte[] buf = new byte[32 * 1024];
28:
29: for (;;) {
30: int len = is.read(buf);
31:
32: if (len < 1)
33: break;
34:
35: bv.add(buf, len);
36: }
37:
38: is.close();
39:
40: return bv.getData();
41: }
42:
43: private class Packet {
44: private byte[] data = null;
45:
46: Packet(byte[] d) {
47: data = d;
48: }
49:
50: public byte[] getData() {
51: return data;
52: }
53: }
54:
55: private Vector vPacket = null;
56: private int sizeInByte = 0;
57:
58: public ByteVector() {
59: vPacket = new Vector();
60: sizeInByte = 0;
61: }
62:
63: public int size() {
64: return sizeInByte;
65: }
66:
67: public void add(byte[] d) {
68: vPacket.add(new Packet(d));
69: sizeInByte += d.length;
70: }
71:
72: public void add(byte[] d, int len) {
73: byte[] buf = new byte[len];
74: System.arraycopy(d, 0, buf, 0, len);
75:
76: vPacket.add(new Packet(buf));
77: sizeInByte += len;
78: }
79:
80: public byte[] getData() {
81: byte[] data = new byte[sizeInByte];
82:
83: int idx = 0;
84: for (int i = 0; i < vPacket.size(); i++) {
85: Packet p = (Packet) vPacket.elementAt(i);
86: byte[] pData = p.getData();
87:
88: System.arraycopy(pData, 0, data, idx, pData.length);
89: idx += pData.length;
90: }
91:
92: return data;
93: }
94: }
|