From 99e611fc22bbcc4008bc48df3b6813be1ec9f201 Mon Sep 17 00:00:00 2001 From: mdb Date: Fri, 1 Feb 2002 18:19:43 +0000 Subject: [PATCH] Created ByteArrayOutInputStream for writing data to a byte array output stream and then reading it back via an InputStream without having to copy the data to a separate buffer. git-svn-id: https://samskivert.googlecode.com/svn/trunk@551 6335cc39-0255-0410-8fd6-9bcaacd3b74c --- .../io/ByteArrayOutInputStream.java | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 projects/samskivert/src/java/com/samskivert/io/ByteArrayOutInputStream.java diff --git a/projects/samskivert/src/java/com/samskivert/io/ByteArrayOutInputStream.java b/projects/samskivert/src/java/com/samskivert/io/ByteArrayOutInputStream.java new file mode 100644 index 00000000..d3efaac6 --- /dev/null +++ b/projects/samskivert/src/java/com/samskivert/io/ByteArrayOutInputStream.java @@ -0,0 +1,65 @@ +// +// $Id: ByteArrayOutInputStream.java,v 1.1 2002/02/01 18:19:43 mdb Exp $ +// +// samskivert library - useful routines for java programs +// Copyright (C) 2001 Michael Bayne +// +// This library is free software; you can redistribute it and/or modify it +// under the terms of the GNU Lesser General Public License as published +// by the Free Software Foundation; either version 2.1 of the License, or +// (at your option) any later version. +// +// This library 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 +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +package com.samskivert.io; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; + +/** + * The byte array out/input stream is used for writing data to a byte + * array output stream and then obtaining an input stream that can read + * back the data written to the output stream without first having to copy + * it to a separate buffer. + */ +public class ByteArrayOutInputStream extends ByteArrayOutputStream +{ + /** + * Creates a new byte array out/input stream. The buffer capacity is + * initially 32 bytes, though its size increases if necessary. + */ + public ByteArrayOutInputStream () + { + this(32); + } + + /** + * Creates a new byte array out/input stream, with a buffer capacity + * of the specified size, in bytes. + * + * @param size the initial size. + * + * @exception IllegalArgumentException if size is negative. + */ + public ByteArrayOutInputStream (int size) + { + super(size); + } + + /** + * Returns an input stream configured to read only the bytes that have + * been written this far to this byte array out/input stream. + */ + public InputStream getInputStream () + { + return new ByteArrayInputStream(buf, 0, count); + } +}