bayard 2002/11/11 11:34:03
Added: io/src/java/org/apache/commons/io/input
ClassLoaderObjectInputStream.java
CountingInputStream.java DemuxInputStream.java
SwappedDataInputStream.java
io/src/java/org/apache/commons/io/output
CountingOutputStream.java DemuxOutputStream.java
LockableFileWriter.java TeeOutputStream.java
Removed: io/src/java/org/apache/commons/io
ClassLoaderObjectInputStream.java
CountingInputStream.java CountingOutputStream.java
DemuxInputStream.java DemuxOutputStream.java
LockableFileWriter.java SwappedDataInputStream.java
TeeOutputStream.java
Log:
InputStreams moved into input.
OutputStreams moved into output.
Writers moved into output.
Revision Changes Path
1.1
jakarta-commons-sandbox/io/src/java/org/apache/commons/io/input/ClassLoaderObjectInputStream.java
Index: ClassLoaderObjectInputStream.java
===================================================================
package org.apache.commons.io.input;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectStreamClass;
import java.io.StreamCorruptedException;
/**
* A special ObjectInputStream to handle highly transient classes hosted
* by Avalon components that are juggling many classloaders.
*
* @author <a href="mailto:paul_hammant@;yahoo.com">Paul Hammant</a>
* @version $Revision: 1.1 $ $Date: 2002/11/11 19:34:02 $
*/
public class ClassLoaderObjectInputStream
extends ObjectInputStream
{
private ClassLoader m_classLoader;
public ClassLoaderObjectInputStream( final ClassLoader classLoader,
final InputStream inputStream )
throws IOException, StreamCorruptedException
{
super( inputStream );
m_classLoader = classLoader;
}
protected Class resolveClass( final ObjectStreamClass objectStreamClass )
throws IOException, ClassNotFoundException
{
final Class clazz =
Class.forName( objectStreamClass.getName(), false, m_classLoader );
if( null != clazz )
{
return clazz; // the classloader knows of the class
}
else
{
// classloader knows not of class, let the super classloader do it
return super.resolveClass( objectStreamClass );
}
}
}
1.1
jakarta-commons-sandbox/io/src/java/org/apache/commons/io/input/CountingInputStream.java
Index: CountingInputStream.java
===================================================================
package org.apache.commons.io.input;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.FilterInputStream;
/**
* Used in debugging, it counts the number of bytes that pass
* through it.
*
* @author <a href="mailto:bayard@;apache.org">Henri Yandell</a>
* @version $Id: CountingInputStream.java,v 1.1 2002/11/11 19:34:02 bayard Exp $
*/
public class CountingInputStream extends FilterInputStream {
private int count;
public CountingInputStream( InputStream in ) {
super(in);
}
public int read(byte[] b) throws IOException {
count += b.length;
return super.read(b);
}
public int read(byte[] b, int off, int len) throws IOException {
count += len;
return super.read(b, off, len);
}
/// TODO: Decide if this should increment by 2, or 4, or 1 etc.
public int read() throws IOException {
count++;
return super.read();
}
/**
* The number of bytes that have passed through this stream.
*/
public int getCount() {
return this.count;
}
}
1.1
jakarta-commons-sandbox/io/src/java/org/apache/commons/io/input/DemuxInputStream.java
Index: DemuxInputStream.java
===================================================================
package org.apache.commons.io.input;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.IOException;
import java.io.InputStream;
/**
* Data written to this stream is forwarded to a stream that has been associated
* with this thread.
*
* @author <a href="mailto:peter@;apache.org">Peter Donald</a>
* @version $Revision: 1.1 $ $Date: 2002/11/11 19:34:02 $
*/
public final class DemuxInputStream
extends InputStream
{
private final InheritableThreadLocal m_streams = new InheritableThreadLocal();
/**
* Bind the specified stream to the current thread.
*
* @param input the stream to bind
*/
public InputStream bindStream( final InputStream input )
{
final InputStream oldValue = getStream();
m_streams.set( input );
return oldValue;
}
/**
* Closes stream associated with current thread.
*
* @throws IOException if an error occurs
*/
public void close()
throws IOException
{
final InputStream input = getStream();
if( null != input )
{
input.close();
}
}
/**
* Read byte from stream associated with current thread.
*
* @return the byte read from stream
* @throws IOException if an error occurs
*/
public int read()
throws IOException
{
final InputStream input = getStream();
if( null != input )
{
return input.read();
}
else
{
return -1;
}
}
/**
* Utility method to retrieve stream bound to current thread (if any).
*/
private InputStream getStream()
{
return (InputStream)m_streams.get();
}
}
1.1
jakarta-commons-sandbox/io/src/java/org/apache/commons/io/input/SwappedDataInputStream.java
Index: SwappedDataInputStream.java
===================================================================
package org.apache.commons.io.input;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.DataInput;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.EndianUtil;
/**
* DataInput for systems relying on little endian data formats.
*
* @author <a href="mailto:peter@;apache.org">Peter Donald</a>
* @version CVS $Revision: 1.1 $ $Date: 2002/11/11 19:34:02 $
* @since 4.0
*/
public class SwappedDataInputStream
implements DataInput
{
//The underlying input stream
private InputStream m_input;
public SwappedDataInputStream( final InputStream input )
{
m_input = input;
}
public boolean readBoolean()
throws IOException, EOFException
{
return ( 0 == readByte() );
}
public byte readByte()
throws IOException, EOFException
{
return (byte)m_input.read();
}
public char readChar()
throws IOException, EOFException
{
return (char)readShort();
}
public double readDouble()
throws IOException, EOFException
{
return EndianUtil.readSwappedDouble( m_input );
}
public float readFloat()
throws IOException, EOFException
{
return EndianUtil.readSwappedFloat( m_input );
}
public void readFully( final byte[] data )
throws IOException, EOFException
{
readFully( data, 0, data.length );
}
public void readFully( final byte[] data, final int offset, final int length )
throws IOException, EOFException
{
int remaining = length;
while( remaining > 0 )
{
final int location = offset + ( length - remaining );
final int count = read( data, location, remaining );
if( -1 == count )
{
throw new EOFException();
}
remaining -= count;
}
}
public int readInt()
throws IOException, EOFException
{
return EndianUtil.readSwappedInteger( m_input );
}
public String readLine()
throws IOException, EOFException
{
throw new IOException( "Operation not supported" );
}
public long readLong()
throws IOException, EOFException
{
return EndianUtil.readSwappedLong( m_input );
}
public short readShort()
throws IOException, EOFException
{
return EndianUtil.readSwappedShort( m_input );
}
public int readUnsignedByte()
throws IOException, EOFException
{
return m_input.read();
}
public int readUnsignedShort()
throws IOException, EOFException
{
return EndianUtil.readSwappedUnsignedShort( m_input );
}
public String readUTF()
throws IOException, EOFException
{
throw new IOException( "Operation not supported" );
}
public int skipBytes( final int count )
throws IOException, EOFException
{
return (int)m_input.skip( count );
}
public int available()
throws IOException, EOFException
{
return m_input.available();
}
public void close()
throws IOException, EOFException
{
m_input.close();
}
public int read()
throws IOException, EOFException
{
return m_input.read();
}
public int read( final byte[] data )
throws IOException, EOFException
{
return read( data, 0, data.length );
}
public int read( final byte[] data, final int offset, final int length )
throws IOException, EOFException
{
return m_input.read( data, offset, length );
}
public long skip( final long count )
throws IOException, EOFException
{
return m_input.skip( count );
}
public void mark( final int readLimit )
{
m_input.mark( readLimit );
}
public boolean markSupported()
{
return m_input.markSupported();
}
public void reset()
throws IOException
{
m_input.reset();
}
}
1.1
jakarta-commons-sandbox/io/src/java/org/apache/commons/io/output/CountingOutputStream.java
Index: CountingOutputStream.java
===================================================================
package org.apache.commons.io.output;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.IOException;
import java.io.OutputStream;
import java.io.FilterOutputStream;
/**
* Used in debugging, it counts the number of bytes that pass
* through it.
*
* @author <a href="mailto:bayard@;apache.org">Henri Yandell</a>
* @version $Id: CountingOutputStream.java,v 1.1 2002/11/11 19:34:02 bayard Exp $
*/
public class CountingOutputStream extends FilterOutputStream {
private int count;
public CountingOutputStream( OutputStream out ) {
super(out);
}
public void write(byte[] b) throws IOException {
count += b.length;
super.write(b);
}
public void write(byte[] b, int off, int len) throws IOException {
count += len;
super.write(b, off, len);
}
/// TODO: Decide if this should increment by 2, or 4, or 1 etc.
public void write(int b) throws IOException {
count++;
super.write(b);
}
/**
* The number of bytes that have passed through this stream.
*/
public int getCount() {
return this.count;
}
}
1.1
jakarta-commons-sandbox/io/src/java/org/apache/commons/io/output/DemuxOutputStream.java
Index: DemuxOutputStream.java
===================================================================
package org.apache.commons.io.output;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.IOException;
import java.io.OutputStream;
/**
* Data written to this stream is forwarded to a stream that has been associated
* with this thread.
*
* @author <a href="mailto:peter@;apache.org">Peter Donald</a>
* @version $Revision: 1.1 $ $Date: 2002/11/11 19:34:02 $
*/
public final class DemuxOutputStream
extends OutputStream
{
private final InheritableThreadLocal m_streams = new InheritableThreadLocal();
/**
* Bind the specified stream to the current thread.
*
* @param output the stream to bind
*/
public OutputStream bindStream( final OutputStream output )
{
final OutputStream stream = getStream();
m_streams.set( output );
return stream;
}
/**
* Closes stream associated with current thread.
*
* @throws IOException if an error occurs
*/
public void close()
throws IOException
{
final OutputStream output = getStream();
if( null != output )
{
output.close();
}
}
/**
* Flushes stream associated with current thread.
*
* @throws IOException if an error occurs
*/
public void flush()
throws IOException
{
final OutputStream output = getStream();
if( null != output )
{
output.flush();
}
}
/**
* Writes byte to stream associated with current thread.
*
* @param ch the byte to write to stream
* @throws IOException if an error occurs
*/
public void write( final int ch )
throws IOException
{
final OutputStream output = getStream();
if( null != output )
{
output.write( ch );
}
}
/**
* Utility method to retrieve stream bound to current thread (if any).
*/
private OutputStream getStream()
{
return (OutputStream)m_streams.get();
}
}
1.1
jakarta-commons-sandbox/io/src/java/org/apache/commons/io/output/LockableFileWriter.java
Index: LockableFileWriter.java
===================================================================
package org.apache.commons.io.output;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* FileWriter that will create and honor lock files to allow simple
* cross thread file lock handling. If <code>Writer</code> attributes
* are unspecified, the default behavior is to overwrite (rather than
* to append), and to use the value of the system property
* <code>java.io.tmpdir</code> for the lock file directory.
*
* Borrowed from the commons-util repo.
*
* @author <a href="mailto:sanders@;apache.org">Scott Sanders</a>
* @author <a href="mailto:ms@;collab.net">Michael Salmon</a>
* @author <a href="mailto:jon@;collab.net">Jon S. Stevens</a>
* @author <a href="mailto:dlr@;finemaltcoding.com">Daniel Rall</a>
* @version $Id: LockableFileWriter.java,v 1.1 2002/11/11 19:34:02 bayard Exp $
*/
public class LockableFileWriter extends Writer {
private static final String LCK = ".lck";
private File lockFile = null;
private FileWriter writer = null;
private boolean append = false;
public LockableFileWriter(String fileName)
throws IOException {
this(fileName, false, null);
}
public LockableFileWriter(String fileName, boolean append)
throws IOException {
this(fileName, append, null);
}
public LockableFileWriter(String fileName, boolean append, String lockDir)
throws IOException {
this(new File(fileName), append, lockDir);
}
public LockableFileWriter(File file)
throws IOException {
this(file, false, null);
}
public LockableFileWriter(File file, boolean append)
throws IOException {
this(file, append, null);
}
public LockableFileWriter(File file, boolean append, String lockDir)
throws IOException {
this.append = append;
if (lockDir == null) {
lockDir = System.getProperty("java.io.tmpdir");
}
testLockDir(new File(lockDir));
this.lockFile = new File(lockDir, file.getName() + LCK);
createLock();
this.writer = new FileWriter(file.getAbsolutePath(), this.append);
}
private void testLockDir(File lockDir)
throws IOException {
if (!lockDir.exists()) {
throw new IOException(
"Could not find lockDir: " + lockDir.getAbsolutePath());
}
if (!lockDir.canWrite()) {
throw new IOException(
"Could not write to lockDir: " + lockDir.getAbsolutePath());
}
}
private void createLock()
throws IOException {
synchronized (LockableFileWriter.class) {
if (!lockFile.createNewFile()) {
throw new IOException("Can't write file, lock " +
lockFile.getAbsolutePath() + " exists");
}
lockFile.deleteOnExit();
}
}
public void close()
throws IOException {
try {
writer.close();
} finally {
lockFile.delete();
}
}
public void write(char[] cbuf, int off, int len)
throws IOException {
writer.write(cbuf, off, len);
}
public void flush()
throws IOException {
writer.flush();
}
}
1.1
jakarta-commons-sandbox/io/src/java/org/apache/commons/io/output/TeeOutputStream.java
Index: TeeOutputStream.java
===================================================================
package org.apache.commons.io.output;
/* ====================================================================
* The Apache Software License, Version 1.1
*
* Copyright (c) 2001 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Apache" and "Apache Software Foundation" and
* "Apache Turbine" must not be used to endorse or promote products
* derived from this software without prior written permission. For
* written permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache",
* "Apache Turbine", nor may "Apache" appear in their name, without
* prior written permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
import java.io.IOException;
import java.io.OutputStream;
import java.io.FilterOutputStream;
/**
* Classic splitter of OutputStream. Named after the unix 'tee'
* command. It allows a stream to be branched off so there
* are now two streams.
*
* @author <a href="mailto:bayard@;apache.org">Henri Yandell</a>
* @version $Id: TeeOutputStream.java,v 1.1 2002/11/11 19:34:02 bayard Exp $
*/
public class TeeOutputStream extends FilterOutputStream {
protected OutputStream branch;
public TeeOutputStream( OutputStream out, OutputStream branch ) {
super(out);
this.branch = branch;
}
public synchronized void write(byte[] b, int off, int len) throws IOException {
super.write(b, off, len);
this.branch.write(b, off, len);
}
public synchronized void write(int b) throws IOException {
super.write(b);
this.branch.write(b);
}
public void flush() throws IOException {
super.flush();
branch.flush();
}
public void close() throws IOException {
super.close();
branch.close();
}
}
--
To unsubscribe, e-mail: <mailto:commons-dev-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@;jakarta.apache.org>