summaryrefslogtreecommitdiff
path: root/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/utils/StreamUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/utils/StreamUtils.java')
-rw-r--r--eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/utils/StreamUtils.java177
1 files changed, 83 insertions, 94 deletions
diff --git a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/utils/StreamUtils.java b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/utils/StreamUtils.java
index 530da777..cc784870 100644
--- a/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/utils/StreamUtils.java
+++ b/eaaf_core_utils/src/main/java/at/gv/egiz/eaaf/core/impl/utils/StreamUtils.java
@@ -1,29 +1,22 @@
-/*******************************************************************************
- * Copyright 2017 Graz University of Technology
- * EAAF-Core Components has been developed in a cooperation between EGIZ,
- * A-SIT Plus, A-SIT, and Graz University of Technology.
+/*
+ * Copyright 2017 Graz University of Technology EAAF-Core Components has been developed in a
+ * cooperation between EGIZ, A-SIT Plus, A-SIT, and Graz University of Technology.
*
- * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
- * the European Commission - subsequent versions of the EUPL (the "Licence");
- * You may not use this work except in compliance with the Licence.
- * You may obtain a copy of the Licence at:
+ * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European
+ * Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work except in
+ * compliance with the Licence. You may obtain a copy of the Licence at:
* https://joinup.ec.europa.eu/news/understanding-eupl-v12
*
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the Licence is distributed on an "AS IS" basis,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the Licence for the specific language governing permissions and
- * limitations under the Licence.
- *
- * This product combines work with different licenses. See the "NOTICE" text
- * file for details on the various modules and licenses.
- * The "NOTICE" text file is part of the distribution. Any derivative works
- * that you distribute must include a readable copy of the "NOTICE" text file.
- *******************************************************************************/
-/*******************************************************************************
- *******************************************************************************/
-/*******************************************************************************
- *******************************************************************************/
+ * Unless required by applicable law or agreed to in writing, software distributed under the Licence
+ * is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
+ * or implied. See the Licence for the specific language governing permissions and limitations under
+ * the Licence.
+ *
+ * This product combines work with different licenses. See the "NOTICE" text file for details on the
+ * various modules and licenses. The "NOTICE" text file is part of the distribution. Any derivative
+ * works that you distribute must include a readable copy of the "NOTICE" text file.
+ */
+
package at.gv.egiz.eaaf.core.impl.utils;
@@ -32,38 +25,37 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.io.PrintStream;
/**
* Utility methods for streams.
- *
+ *
* @author Patrick Peck
* @version $Id$
*/
public class StreamUtils {
-
+
/**
* Compare the contents of two <code>InputStream</code>s.
- *
+ *
* @param is1 The 1st <code>InputStream</code> to compare.
* @param is2 The 2nd <code>InputStream</code> to compare.
- * @return boolean <code>true</code>, if both streams contain the exactly the
- * same content, <code>false</code> otherwise.
+ * @return boolean <code>true</code>, if both streams contain the exactly the same content,
+ * <code>false</code> otherwise.
* @throws IOException An error occurred reading one of the streams.
*/
- public static boolean compareStreams(InputStream is1, InputStream is2)
- throws IOException {
-
- byte[] buf1 = new byte[256];
- byte[] buf2 = new byte[256];
+ public static boolean compareStreams(final InputStream is1, final InputStream is2)
+ throws IOException {
+
+ final byte[] buf1 = new byte[256];
+ final byte[] buf2 = new byte[256];
int length1;
int length2;
-
+
try {
while (true) {
length1 = is1.read(buf1);
length2 = is2.read(buf2);
-
+
if (length1 != length2) {
return false;
}
@@ -74,128 +66,125 @@ public class StreamUtils {
return false;
}
}
- } catch (IOException e) {
+ } catch (final IOException e) {
throw e;
} finally {
// close both streams
try {
is1.close();
is2.close();
- } catch (IOException e) {
- // ignore this
+ } catch (final IOException e) {
+ e.printStackTrace();
+
}
}
}
-
+
/**
* Compare two byte arrays, up to a given maximum length.
- *
+ *
* @param b1 1st byte array to compare.
* @param b2 2nd byte array to compare.
* @param length The maximum number of bytes to compare.
- * @return <code>true</code>, if the byte arrays are equal, <code>false</code>
- * otherwise.
+ * @return <code>true</code>, if the byte arrays are equal, <code>false</code> otherwise.
*/
- private static boolean compareBytes(byte[] b1, byte[] b2, int length) {
+ private static boolean compareBytes(final byte[] b1, final byte[] b2, final int length) {
if (b1.length != b2.length) {
return false;
}
-
+
for (int i = 0; i < b1.length && i < length; i++) {
if (b1[i] != b2[i]) {
return false;
}
}
-
+
return true;
}
/**
* Reads a byte array from a stream.
+ *
* @param in The <code>InputStream</code> to read.
* @return The bytes contained in the given <code>InputStream</code>.
* @throws IOException on any exception thrown
*/
- public static byte[] readStream(InputStream in) throws IOException {
+ public static byte[] readStream(final InputStream in) throws IOException {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
+ final ByteArrayOutputStream out = new ByteArrayOutputStream();
copyStream(in, out, null);
-
- /*
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- int b;
- while ((b = in.read()) >= 0)
- out.write(b);
-
- */
+
+ /*
+ * ByteArrayOutputStream out = new ByteArrayOutputStream(); int b; while ((b = in.read()) >= 0)
+ * out.write(b);
+ *
+ */
in.close();
return out.toByteArray();
}
/**
* Reads a <code>String</code> from a stream, using given encoding.
+ *
* @param in The <code>InputStream</code> to read.
- * @param encoding The character encoding to use for converting the bytes
- * of the <code>InputStream</code> into a <code>String</code>.
- * @return The content of the given <code>InputStream</code> converted into
- * a <code>String</code>.
+ * @param encoding The character encoding to use for converting the bytes of the
+ * <code>InputStream</code> into a <code>String</code>.
+ * @return The content of the given <code>InputStream</code> converted into a <code>String</code>.
* @throws IOException on any exception thrown
*/
- public static String readStream(InputStream in, String encoding) throws IOException {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
+ public static String readStream(final InputStream in, final String encoding) throws IOException {
+ final ByteArrayOutputStream out = new ByteArrayOutputStream();
copyStream(in, out, null);
/*
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- int b;
- while ((b = in.read()) >= 0)
- out.write(b);
- */
+ * ByteArrayOutputStream out = new ByteArrayOutputStream(); int b; while ((b = in.read()) >= 0)
+ * out.write(b);
+ */
in.close();
return out.toString(encoding);
}
-
+
/**
- * Reads all data (until EOF is reached) from the given source to the
- * destination stream. If the destination stream is null, all data is dropped.
- * It uses the given buffer to read data and forward it. If the buffer is
- * null, this method allocates a buffer.
+ * Reads all data (until EOF is reached) from the given source to the destination stream. If the
+ * destination stream is null, all data is dropped. It uses the given buffer to read data and
+ * forward it. If the buffer is null, this method allocates a buffer.
*
* @param source The stream providing the data.
- * @param destination The stream that takes the data. If this is null, all
- * data from source will be read and discarded.
- * @param buffer The buffer to use for forwarding. If it is null, the method
- * allocates a buffer.
- * @exception IOException If reading from the source or writing to the
- * destination fails.
+ * @param destination The stream that takes the data. If this is null, all data from source will
+ * be read and discarded.
+ * @param buffer The buffer to use for forwarding. If it is null, the method allocates a buffer.
+ * @exception IOException If reading from the source or writing to the destination fails.
*/
- private static void copyStream(InputStream source, OutputStream destination, byte[] buffer) throws IOException {
+ private static void copyStream(final InputStream source, final OutputStream destination,
+ byte[] buffer) throws IOException {
if (source == null) {
throw new NullPointerException("Argument \"source\" must not be null.");
}
if (buffer == null) {
buffer = new byte[8192];
}
-
+
if (destination != null) {
int bytesRead;
while ((bytesRead = source.read(buffer)) >= 0) {
destination.write(buffer, 0, bytesRead);
}
} else {
- while (source.read(buffer) >= 0);
- }
- }
-
- /**
- * Gets the stack trace of the <code>Throwable</code> passed in as a string.
- * @param t The <code>Throwable</code>.
- * @return a String representing the stack trace of the <code>Throwable</code>.
- */
- public static String getStackTraceAsString(Throwable t)
- {
- ByteArrayOutputStream stackTraceBIS = new ByteArrayOutputStream();
- t.printStackTrace(new PrintStream(stackTraceBIS));
- return new String(stackTraceBIS.toByteArray());
+ while (source.read(buffer) >= 0) {
+
+ }
+ }
}
+
+ // /**
+ // * Gets the stack trace of the <code>Throwable</code> passed in as a string.
+ // *
+ // * @param t The <code>Throwable</code>.
+ // * @return a String representing the stack trace of the <code>Throwable</code>.
+ // */
+ // public static String getStackTraceAsString(final Throwable t) {
+ // final ByteArrayOutputStream stackTraceBis = new ByteArrayOutputStream();
+ // t.printStackTrace(new PrintStream(stackTraceBis));
+ // return new String(stackTraceBis.toByteArray());
+ // }
}