Skip to content

Commit c360a10

Browse files
author
Nicola Ken Barozzi
committed
New io classes from Avalon excalibur.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@140301 13f79535-47bb-0310-9956-ffa450edef68
1 parent c358911 commit c360a10

14 files changed

Lines changed: 3429 additions & 0 deletions
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.apache.commons.io;
2+
3+
/* ====================================================================
4+
* The Apache Software License, Version 1.1
5+
*
6+
* Copyright (c) 2001 The Apache Software Foundation. All rights
7+
* reserved.
8+
*
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions
11+
* are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions and the following disclaimer.
15+
*
16+
* 2. Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in
18+
* the documentation and/or other materials provided with the
19+
* distribution.
20+
*
21+
* 3. The end-user documentation included with the redistribution,
22+
* if any, must include the following acknowledgment:
23+
* "This product includes software developed by the
24+
* Apache Software Foundation (http://www.apache.org/)."
25+
* Alternately, this acknowledgment may appear in the software itself,
26+
* if and wherever such third-party acknowledgments normally appear.
27+
*
28+
* 4. The names "Apache" and "Apache Software Foundation" and
29+
* "Apache Turbine" must not be used to endorse or promote products
30+
* derived from this software without prior written permission. For
31+
* written permission, please contact apache@apache.org.
32+
*
33+
* 5. Products derived from this software may not be called "Apache",
34+
* "Apache Turbine", nor may "Apache" appear in their name, without
35+
* prior written permission of the Apache Software Foundation.
36+
*
37+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40+
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48+
* SUCH DAMAGE.
49+
* ====================================================================
50+
*
51+
* This software consists of voluntary contributions made by many
52+
* individuals on behalf of the Apache Software Foundation. For more
53+
* information on the Apache Software Foundation, please see
54+
* <http://www.apache.org/>.
55+
*/
56+
57+
import java.io.File;
58+
import java.io.FilenameFilter;
59+
60+
/**
61+
* Accepts a selection if it is acceptable to both of two {@link FilenameFilter}s.
62+
* This takes two {@link FilenameFilter}s as input.
63+
*
64+
* <p>Eg., to print all files beginning with <code>A</code> and ending with <code>.java</code>:</p>
65+
*
66+
* <pre>
67+
* File dir = new File(".");
68+
* String[] files = dir.list( new AndFileFilter(
69+
* new PrefixFileFilter("A"),
70+
* new ExtensionFileFilter(".java")
71+
* )
72+
* );
73+
* for ( int i=0; i&lt;files.length; i++ )
74+
* {
75+
* System.out.println(files[i]);
76+
* }
77+
* </pre>
78+
*
79+
* @author Harmeet Bedi <harmeet@kodemuse.com>
80+
* @version $Revision: 1.1 $ $Date: 2002/07/08 22:14:46 $
81+
* @since 4.0
82+
*/
83+
public class AndFileFilter
84+
implements FilenameFilter
85+
{
86+
private final FilenameFilter m_filter1;
87+
private final FilenameFilter m_filter2;
88+
89+
public AndFileFilter( final FilenameFilter filter1, final FilenameFilter filter2 )
90+
{
91+
m_filter1 = filter1;
92+
m_filter2 = filter2;
93+
}
94+
95+
public boolean accept( final File file, final String name )
96+
{
97+
return m_filter1.accept( file, name ) && m_filter2.accept( file, name );
98+
}
99+
}
100+
101+
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package org.apache.commons.io;
2+
3+
/* ====================================================================
4+
* The Apache Software License, Version 1.1
5+
*
6+
* Copyright (c) 2001 The Apache Software Foundation. All rights
7+
* reserved.
8+
*
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions
11+
* are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions and the following disclaimer.
15+
*
16+
* 2. Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in
18+
* the documentation and/or other materials provided with the
19+
* distribution.
20+
*
21+
* 3. The end-user documentation included with the redistribution,
22+
* if any, must include the following acknowledgment:
23+
* "This product includes software developed by the
24+
* Apache Software Foundation (http://www.apache.org/)."
25+
* Alternately, this acknowledgment may appear in the software itself,
26+
* if and wherever such third-party acknowledgments normally appear.
27+
*
28+
* 4. The names "Apache" and "Apache Software Foundation" and
29+
* "Apache Turbine" must not be used to endorse or promote products
30+
* derived from this software without prior written permission. For
31+
* written permission, please contact apache@apache.org.
32+
*
33+
* 5. Products derived from this software may not be called "Apache",
34+
* "Apache Turbine", nor may "Apache" appear in their name, without
35+
* prior written permission of the Apache Software Foundation.
36+
*
37+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40+
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48+
* SUCH DAMAGE.
49+
* ====================================================================
50+
*
51+
* This software consists of voluntary contributions made by many
52+
* individuals on behalf of the Apache Software Foundation. For more
53+
* information on the Apache Software Foundation, please see
54+
* <http://www.apache.org/>.
55+
*/
56+
57+
import java.io.IOException;
58+
import java.io.InputStream;
59+
import java.io.ObjectInputStream;
60+
import java.io.ObjectStreamClass;
61+
import java.io.StreamCorruptedException;
62+
63+
/**
64+
* A special ObjectInputStream to handle highly transient classes hosted
65+
* by Avalon components that are juggling many classloaders.
66+
*
67+
* @author <a href="mailto:paul_hammant@yahoo.com">Paul Hammant</a>
68+
* @version $Revision: 1.1 $ $Date: 2002/07/08 22:14:46 $
69+
*/
70+
public class ClassLoaderObjectInputStream
71+
extends ObjectInputStream
72+
{
73+
private ClassLoader m_classLoader;
74+
75+
public ClassLoaderObjectInputStream( final ClassLoader classLoader,
76+
final InputStream inputStream )
77+
throws IOException, StreamCorruptedException
78+
{
79+
super( inputStream );
80+
m_classLoader = classLoader;
81+
}
82+
83+
protected Class resolveClass( final ObjectStreamClass objectStreamClass )
84+
throws IOException, ClassNotFoundException
85+
{
86+
final Class clazz =
87+
Class.forName( objectStreamClass.getName(), false, m_classLoader );
88+
89+
if( null != clazz )
90+
{
91+
return clazz; // the classloader knows of the class
92+
}
93+
else
94+
{
95+
// classloader knows not of class, let the super classloader do it
96+
return super.resolveClass( objectStreamClass );
97+
}
98+
}
99+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package org.apache.commons.io;
2+
3+
/* ====================================================================
4+
* The Apache Software License, Version 1.1
5+
*
6+
* Copyright (c) 2001 The Apache Software Foundation. All rights
7+
* reserved.
8+
*
9+
* Redistribution and use in source and binary forms, with or without
10+
* modification, are permitted provided that the following conditions
11+
* are met:
12+
*
13+
* 1. Redistributions of source code must retain the above copyright
14+
* notice, this list of conditions and the following disclaimer.
15+
*
16+
* 2. Redistributions in binary form must reproduce the above copyright
17+
* notice, this list of conditions and the following disclaimer in
18+
* the documentation and/or other materials provided with the
19+
* distribution.
20+
*
21+
* 3. The end-user documentation included with the redistribution,
22+
* if any, must include the following acknowledgment:
23+
* "This product includes software developed by the
24+
* Apache Software Foundation (http://www.apache.org/)."
25+
* Alternately, this acknowledgment may appear in the software itself,
26+
* if and wherever such third-party acknowledgments normally appear.
27+
*
28+
* 4. The names "Apache" and "Apache Software Foundation" and
29+
* "Apache Turbine" must not be used to endorse or promote products
30+
* derived from this software without prior written permission. For
31+
* written permission, please contact apache@apache.org.
32+
*
33+
* 5. Products derived from this software may not be called "Apache",
34+
* "Apache Turbine", nor may "Apache" appear in their name, without
35+
* prior written permission of the Apache Software Foundation.
36+
*
37+
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38+
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39+
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40+
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41+
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44+
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45+
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48+
* SUCH DAMAGE.
49+
* ====================================================================
50+
*
51+
* This software consists of voluntary contributions made by many
52+
* individuals on behalf of the Apache Software Foundation. For more
53+
* information on the Apache Software Foundation, please see
54+
* <http://www.apache.org/>.
55+
*/
56+
57+
import java.io.IOException;
58+
import java.io.InputStream;
59+
60+
/**
61+
* Data written to this stream is forwarded to a stream that has been associated
62+
* with this thread.
63+
*
64+
* @author <a href="mailto:peter@apache.org">Peter Donald</a>
65+
* @version $Revision: 1.1 $ $Date: 2002/07/08 22:14:46 $
66+
*/
67+
public final class DemuxInputStream
68+
extends InputStream
69+
{
70+
private final InheritableThreadLocal m_streams = new InheritableThreadLocal();
71+
72+
/**
73+
* Bind the specified stream to the current thread.
74+
*
75+
* @param input the stream to bind
76+
*/
77+
public InputStream bindStream( final InputStream input )
78+
{
79+
final InputStream oldValue = getStream();
80+
m_streams.set( input );
81+
return oldValue;
82+
}
83+
84+
/**
85+
* Closes stream associated with current thread.
86+
*
87+
* @throws IOException if an error occurs
88+
*/
89+
public void close()
90+
throws IOException
91+
{
92+
final InputStream input = getStream();
93+
if( null != input )
94+
{
95+
input.close();
96+
}
97+
}
98+
99+
/**
100+
* Read byte from stream associated with current thread.
101+
*
102+
* @return the byte read from stream
103+
* @throws IOException if an error occurs
104+
*/
105+
public int read()
106+
throws IOException
107+
{
108+
final InputStream input = getStream();
109+
if( null != input )
110+
{
111+
return input.read();
112+
}
113+
else
114+
{
115+
return -1;
116+
}
117+
}
118+
119+
/**
120+
* Utility method to retrieve stream bound to current thread (if any).
121+
*/
122+
private InputStream getStream()
123+
{
124+
return (InputStream)m_streams.get();
125+
}
126+
}

0 commit comments

Comments
 (0)