001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: StreamUtil.java,v 1.7 2005/02/16 11:28:14 jesper Exp $
023: package net.infonode.util;
024:
025: import java.io.*;
026:
027: /**
028: * @author $Author: jesper $
029: * @version $Revision: 1.7 $
030: */
031: public class StreamUtil {
032: public static final void readAll(InputStream in, byte[] data,
033: int offset, int length) throws IOException {
034: while (length > 0) {
035: int len = in.read(data, offset, length);
036:
037: if (len == -1)
038: throw new IOException("End of stream reached!");
039:
040: offset += len;
041: length -= len;
042: }
043: }
044:
045: public static final byte[] readAll(InputStream is)
046: throws IOException {
047: byte[] data = new byte[is.available()];
048: int pos = 0;
049:
050: while (pos < data.length)
051: pos += is.read(data, pos, data.length - pos);
052:
053: is.close();
054: return data;
055: }
056:
057: public static final byte[] writeObject(Object object)
058: throws IOException {
059: ByteArrayOutputStream out = new ByteArrayOutputStream();
060: ObjectOutputStream o2 = new ObjectOutputStream(out);
061: o2.writeObject(object);
062: o2.close();
063: return ArrayUtil.part(out.toByteArray(), 0, out.size());
064: }
065:
066: public static final Object readObject(byte[] data)
067: throws IOException, ClassNotFoundException {
068: return new ObjectInputStream(new ByteArrayInputStream(data))
069: .readObject();
070: }
071:
072: public static byte[] write(Writable writable) throws IOException {
073: ByteArrayOutputStream out = new ByteArrayOutputStream();
074: ObjectOutputStream o2 = new ObjectOutputStream(out);
075: writable.write(o2);
076: o2.close();
077: return out.toByteArray();
078: }
079:
080: public static void read(byte[] data, Readable readable)
081: throws IOException {
082: readable.read(new ObjectInputStream(new ByteArrayInputStream(
083: data)));
084: }
085:
086: public static void readAll(InputStream in, byte[] data)
087: throws IOException {
088: readAll(in, data, 0, data.length);
089: }
090:
091: public static void write(InputStream in, OutputStream out,
092: int length) throws IOException {
093: byte[] data = new byte[10000];
094:
095: while (length > 0) {
096: int read = in.read(data, 0, Math.min(data.length, length));
097: out.write(data, 0, read);
098: length -= read;
099: }
100: }
101: }
|