Java/File Input Output/OutputStream
Содержание
A null output stream. All data written to this stream is ignored.
/**
*
* JFreeReport : a free Java reporting library
*
*
* Project Info: http://reporting.pentaho.org/
*
* (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
*
* 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.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------
* NullOutputStream.java
* ------------
* (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
*/
import java.io.IOException;
import java.io.OutputStream;
/**
* A null output stream. All data written to this stream is ignored.
*
* @author Thomas Morgner
*/
public class NullOutputStream extends OutputStream
{
/**
* Default constructor.
*/
public NullOutputStream ()
{
}
/**
* Writes to the stream (in this case, does nothing).
*
* @param i the value.
* @throws IOException if there is an I/O problem.
*/
public void write (final int i)
throws IOException
{
// no i wont do anything here ...
}
/**
* Writes to the stream (in this case, does nothing).
*
* @param bytes the bytes.
* @throws IOException if there is an I/O problem.
*/
public void write (final byte[] bytes)
throws IOException
{
// no i wont do anything here ...
}
/**
* Writes to the stream (in this case, does nothing).
*
* @param bytes the bytes.
* @param off the start offset in the data.
* @param len the number of bytes to write.
* @throws IOException if there is an I/O problem.
*/
public void write (final byte[] bytes, final int off, final int len)
throws IOException
{
// no i wont do anything here ...
}
}
Byte Counting OutputStream
import java.io.IOException;
import java.io.OutputStream;
/**
* Output stream that counts bytes written to it (but discards them).
*
* @author Jonathan Locke
*/
public final class ByteCountingOutputStream extends OutputStream
{
private long size;
/**
* @see java.io.OutputStream#write(int)
*/
public void write(int b) throws IOException
{
size++;
}
/**
* @see java.io.OutputStream#write(byte[], int, int)
*/
public void write(byte b[], int off, int len) throws IOException
{
size += len;
}
/**
* @return Number of bytes written to this stream
*/
public long size()
{
return size;
}
}
Compare the contents of two Streams to determine if they are equal or not.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
/** * Origin of code: Apache Avalon (Excalibur)
*
* @author Peter Donald
* @author Jeff Turner
* @author Matthew Hawthorne
* @author Stephen Colebourne
* @author Gareth Davis
* @version CVS $Revision$ $Date$*/
public class Main {
/**
* Compare the contents of two Streams to determine if they are equal or not.
* <p>
* This method buffers the input internally using <code>BufferedInputStream</code> if they are
* not already buffered.
*
* @param input1
* the first stream
* @param input2
* the second stream
* @return true if the content of the streams are equal or they both don"t exist, false
* otherwise
* @throws NullPointerException
* if either input is null
* @throws IOException
* if an I/O error occurs
*/
public static boolean contentEquals(InputStream input1, InputStream input2) throws IOException
{
if (!(input1 instanceof BufferedInputStream))
{
input1 = new BufferedInputStream(input1);
}
if (!(input2 instanceof BufferedInputStream))
{
input2 = new BufferedInputStream(input2);
}
int ch = input1.read();
while (-1 != ch)
{
int ch2 = input2.read();
if (ch != ch2)
{
return false;
}
ch = input1.read();
}
int ch2 = input2.read();
return (ch2 == -1);
}
}
Memory Byte Array OutputStream
/**
*
* JFreeReport : a free Java reporting library
*
*
* Project Info: http://reporting.pentaho.org/
*
* (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
*
* 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.
*
* [Java is a trademark or registered trademark of Sun Microsystems, Inc.
* in the United States and other countries.]
*
* ------------
* MemoryByteArrayOutputStream.java
* ------------
* (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
*/
import java.io.IOException;
import java.io.OutputStream;
/**
* A string writer that is able to write large amounts of data. The original
* StringWriter contained in Java doubles its buffersize everytime the buffer
* overflows. This is nice with small amounts of data, but awfull for huge
* buffers.
*
* @author Thomas Morgner
*/
public class MemoryByteArrayOutputStream extends OutputStream {
private int maximumBufferIncrement;
private int cursor;
private byte[] buffer;
private byte[] singleIntArray;
/**
* Create a new character-stream writer whose critical sections will
* synchronize on the writer itself.
*/
public MemoryByteArrayOutputStream() {
this(4096, 65536);
}
/**
* Create a new character-stream writer whose critical sections will
* synchronize on the writer itself.
*/
public MemoryByteArrayOutputStream(final int bufferSize, final int maximumBufferIncrement) {
this.maximumBufferIncrement = maximumBufferIncrement;
this.buffer = new byte[bufferSize];
this.singleIntArray = new byte[1];
}
/**
* Write a portion of an array of characters.
*
* @param cbuf
* Array of characters
* @param off
* Offset from which to start writing characters
* @param len
* Number of characters to write
* @throws java.io.IOException
* If an I/O error occurs
*/
public synchronized void write(final byte[] cbuf, final int off, final int len)
throws IOException {
if (len < 0) {
throw new IllegalArgumentException();
}
if (off < 0) {
throw new IndexOutOfBoundsException();
}
if (cbuf == null) {
throw new NullPointerException();
}
if ((len + off) > cbuf.length) {
throw new IndexOutOfBoundsException();
}
ensureSize(cursor + len);
System.arraycopy(cbuf, off, this.buffer, cursor, len);
cursor += len;
}
/**
* Writes <code>b.length</code> bytes from the specified byte array to this
* output stream. The general contract for <code>write(b)</code> is that it
* should have exactly the same effect as the call <code>write(b, 0,
* b.length)</code>.
*
* @param b
* the data.
* @throws java.io.IOException
* if an I/O error occurs.
* @see java.io.OutputStream#write(byte[], int, int)
*/
public void write(final byte[] b) throws IOException {
write(b, 0, b.length);
}
/**
* Writes the specified byte to this output stream. The general contract for
* <code>write</code> is that one byte is written to the output stream. The
* byte to be written is the eight low-order bits of the argument
* <code>b</code>. The 24 high-order bits of <code>b</code> are ignored.
* <p/> Subclasses of <code>OutputStream</code> must provide an
* implementation for this method.
*
* @param b
* the <code>byte</code>.
* @throws java.io.IOException
* if an I/O error occurs. In particular, an
* <code>IOException</code> may be thrown if the output stream has
* been closed.
*/
public synchronized void write(final int b) throws IOException {
this.singleIntArray[0] = (byte) (0xFF & b);
write(singleIntArray, 0, 1);
}
private void ensureSize(final int size) {
if (this.buffer.length >= size) {
return;
}
final int computedSize = (int) Math.min((this.buffer.length + 1) * 1.5, this.buffer.length
+ maximumBufferIncrement);
final int newSize = Math.max(size, computedSize);
final byte[] newBuffer = new byte[newSize];
System.arraycopy(this.buffer, 0, newBuffer, 0, cursor);
this.buffer = newBuffer;
}
/**
* Flush the stream. If the stream has saved any characters from the various
* write() methods in a buffer, write them immediately to their intended
* destination. Then, if that destination is another character or byte stream,
* flush it. Thus one flush() invocation will flush all the buffers in a chain
* of Writers and OutputStreams. <p/> If the intended destination of this
* stream is an abstraction provided by the underlying operating system, for
* example a file, then flushing the stream guarantees only that bytes
* previously written to the stream are passed to the operating system for
* writing; it does not guarantee that they are actually written to a physical
* device such as a disk drive.
*
* @throws java.io.IOException
* If an I/O error occurs
*/
public void flush() throws IOException {
if ((buffer.length - cursor) > 50000) {
System.out.println("WASTED: " + (buffer.length - cursor));
}
}
/**
* Close the stream, flushing it first. Once a stream has been closed, further
* write() or flush() invocations will cause an IOException to be thrown.
* Closing a previously-closed stream, however, has no effect.
*
* @throws java.io.IOException
* If an I/O error occurs
*/
public void close() throws IOException {
}
public synchronized byte[] toByteArray() {
final byte[] retval = new byte[cursor];
System.arraycopy(buffer, 0, retval, 0, cursor);
return retval;
}
public int getLength() {
return cursor;
}
public byte[] getRaw() {
if ((buffer.length - cursor) > 50000) {
System.out.println("WASTED: " + (buffer.length - cursor) + " Length: " + buffer.length);
}
return buffer;
}
}
Read from InputStream and write to OutputStream
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class Main {
/**
* Read input from input stream and write it to output stream
* until there is no more input from input stream.
*
* @param is input stream the input stream to read from.
* @param os output stream the output stream to write to.
* @param buf the byte array to use as a buffer
*/
public static void flow( InputStream is, OutputStream os, byte[] buf )
throws IOException {
int numRead;
while ( (numRead = is.read(buf) ) >= 0) {
os.write(buf, 0, numRead);
}
}
}
String Buffer OutputStream
/*
* Created on Dec 25, 2004
*
* Copyright 2005 CafeSip.org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
import java.io.IOException;
import java.io.OutputStream;
/**
* @author amit
*
*/
public class StringBufferOutputStream extends OutputStream
{
private StringBuffer textBuffer = new StringBuffer();
/**
*
*/
public StringBufferOutputStream()
{
super();
}
/*
* @see java.io.OutputStream#write(int)
*/
public void write(int b) throws IOException
{
char a = (char)b;
textBuffer.append(a);
}
public String toString()
{
return textBuffer.toString();
}
public void clear()
{
textBuffer.delete(0, textBuffer.length());
}
}