001: /*
002: * MCS Media Computer Software Copyright (c) 2005 by MCS
003: * -------------------------------------- Created on 23.04.2005 by w.klaas
004: *
005: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
006: * use this file except in compliance with the License. You may obtain a copy of
007: * the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017: package de.mcs.utils;
018:
019: import java.io.BufferedReader;
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.IOException;
023: import java.io.InputStream;
024: import java.io.InputStreamReader;
025: import java.io.OutputStream;
026: import java.io.Reader;
027:
028: /**
029: * This class contains some helper emthode for streaming operations.
030: *
031: * @author W.Klaas
032: */
033: public final class StreamHelper {
034:
035: /** size of the internal buffer. */
036: private static final int BUFFER_SIZE = 32768;
037:
038: /** no construction . */
039: private StreamHelper() {
040: }
041:
042: /**
043: * copy all data from in to out.
044: *
045: * @param out
046: * outputstream to copy to
047: * @param in
048: * input stream to copy from
049: * @throws IOException
050: * if something goes wrong
051: */
052: public static void copyStream(final InputStream in,
053: final OutputStream out) throws IOException {
054: byte[] buffer = new byte[BUFFER_SIZE];
055: int k = 0;
056: do {
057: k = in.read(buffer);
058: if (k > 0) {
059: out.write(buffer, 0, k);
060: }
061: } while (k >= 0);
062: }
063:
064: /**
065: * copy all data from in to out.
066: *
067: * @param out
068: * outputstream to copy to
069: * @param in
070: * input stream to copy from
071: * @param bufSize
072: * the size of the buffer to use.
073: *
074: * @throws IOException
075: * if something goes wrong
076: */
077: public static void copyStream(final InputStream in,
078: final OutputStream out, final int bufSize)
079: throws IOException {
080: byte[] buffer = new byte[bufSize];
081: int k = 0;
082: do {
083: k = in.read(buffer);
084: if (k > 0) {
085: out.write(buffer, 0, k);
086: }
087: } while (k >= 0);
088: }
089:
090: /**
091: * printing the file to System.out.
092: *
093: * @param rcPropFile
094: * file to print
095: * @throws IOException
096: * if something goes wrong
097: */
098: public static void printOutFile(final File rcPropFile)
099: throws IOException {
100: FileInputStream fileInputStream = new FileInputStream(
101: rcPropFile);
102: Reader reader = new InputStreamReader(fileInputStream);
103: BufferedReader bufferedReader = new BufferedReader(reader);
104: try {
105: String line = null;
106: do {
107: line = bufferedReader.readLine();
108: System.out.println(line);
109: } while (null != line);
110: } finally {
111: bufferedReader.close();
112: }
113: }
114:
115: }
|