diff options
Diffstat (limited to 'sca-java-1.x/tags/java-stable-20060304/sca/common/src/main/java/org/apache/tuscany/common/io/util/IOHelper.java')
-rw-r--r-- | sca-java-1.x/tags/java-stable-20060304/sca/common/src/main/java/org/apache/tuscany/common/io/util/IOHelper.java | 128 |
1 files changed, 128 insertions, 0 deletions
diff --git a/sca-java-1.x/tags/java-stable-20060304/sca/common/src/main/java/org/apache/tuscany/common/io/util/IOHelper.java b/sca-java-1.x/tags/java-stable-20060304/sca/common/src/main/java/org/apache/tuscany/common/io/util/IOHelper.java new file mode 100644 index 0000000000..8227a052df --- /dev/null +++ b/sca-java-1.x/tags/java-stable-20060304/sca/common/src/main/java/org/apache/tuscany/common/io/util/IOHelper.java @@ -0,0 +1,128 @@ +/* + * + * 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. + */ + +package org.apache.tuscany.common.io.util; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * I/O utility methods + * + * @version $Rev$ $Date$ + */ +public class IOHelper { + + // ---------------------------------- + // Fields + // ---------------------------------- + + public static int BYTES = 8192; + + // ---------------------------------- + // Constructors + // ---------------------------------- + + private IOHelper() { + } + + // ---------------------------------- + // Methods + // ---------------------------------- + + public static void copy(InputStream in, OutputStream out) throws IOException { + copy(in, out, -1); + } + + public static void copy(InputStream in, OutputStream out, long byteCount) throws IOException { + byte buffer[] = new byte[BYTES]; + int len = BYTES; + + if (byteCount >= 0) { + while (byteCount > 0) { + if (byteCount < BYTES) { + len = in.read(buffer, 0, (int) byteCount); + } else { + len = in.read(buffer, 0, BYTES); + } + if (len == -1) { + break; + } + byteCount -= len; + out.write(buffer, 0, len); + } + } else { + while (true) { + len = in.read(buffer, 0, BYTES); + if (len < 0) { + break; + } + out.write(buffer, 0, len); + } + } + } + + public static byte[] read(InputStream in) throws IOException { + byte buffer[] = new byte[BYTES]; + ByteArrayOutputStream out = new ByteArrayOutputStream(); + int len = BYTES; + while (true) { + len = in.read(buffer, 0, BYTES); + if (len < 0) { + break; + } + out.write(buffer, 0, len); + } + return out.toByteArray(); + } + + /** + * Removes a directory from the file sytsem + */ + public static boolean deleteDir(File pDir) { + if (pDir.isDirectory()) { + String[] children = pDir.list(); + for (int i = 0; i < children.length; i++) { + boolean success = deleteDir(new File(pDir, children[i])); + if (!success) { + return false; + } + } + } + return pDir.delete(); + } + + /** + * Returns a stream to the resource associated with pPath in the directory + * pRoot + */ + public static InputStream getResource(File pRoot, String pPath) throws FileNotFoundException { + + File[] files = pRoot.listFiles(); + for (int i = 0; i < files.length; i++) { + if (files[i].isFile() && files[i].getName().equals(pPath)) { + return new BufferedInputStream(new FileInputStream(files[i])); + } + } + return null; + } + +} |