-/*******************************************************************************\r
- * Copyright (c) 2010 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- * VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.databoard.util;\r
-\r
-import java.io.EOFException;\r
-import java.io.File;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-import java.io.OutputStream;\r
-import java.nio.ByteBuffer;\r
-import java.nio.channels.Channels;\r
-import java.nio.channels.ReadableByteChannel;\r
-import java.nio.channels.WritableByteChannel;\r
-import java.nio.charset.Charset;\r
-\r
-import org.simantics.databoard.util.binary.BinaryFile;\r
-import org.simantics.databoard.util.binary.BinaryMemory;\r
-\r
-public class StreamUtil {\r
-\r
- public static String readString(InputStream is, Charset cs) throws IOException \r
- {\r
- byte[] data = readFully(is);\r
- return new String(data, cs);\r
- }\r
-\r
- public static String readString(File file, Charset cs) throws IOException \r
- {\r
- byte[] data = readFully(file);\r
- return new String(data, cs);\r
- }\r
- \r
- public static byte[] readFully(InputStream is) throws IOException \r
- {\r
- BinaryMemory mem = new BinaryMemory( 0 );\r
- mem.put(is);\r
- byte[] result = new byte[ (int) mem.length() ];\r
- mem.position(0L);\r
- mem.readFully(result);\r
- return result;\r
- }\r
- \r
- public static byte[] readFully(File file) throws IOException \r
- {\r
- BinaryFile b = new BinaryFile(file);\r
- try {\r
- byte[] bytes = new byte[ (int) b.length() ];\r
- b.readFully(bytes);\r
- return bytes;\r
- } finally {\r
- b.close();\r
- }\r
- }\r
- \r
- \r
- public static void read(InputStream is, ByteBuffer buf, int bytes)\r
- throws IOException\r
- {\r
- while (bytes>0 & buf.hasRemaining()) {\r
- int n = is.read(buf.array(), buf.position(), bytes);\r
- if (n < 0) throw new EOFException();\r
- buf.position( buf.position() + n );\r
- bytes -= n;\r
- }\r
- }\r
- \r
- public static void readFully(InputStream is, ByteBuffer buf)\r
- throws IOException\r
- {\r
- while (buf.hasRemaining()) { \r
- int n = is.read(buf.array(), buf.position(), buf.remaining());\r
- if (n < 0) throw new EOFException();\r
- buf.position( buf.position() + n );\r
- }\r
- }\r
- \r
- public static void readFully(InputStream is, byte[] b)\r
- throws IOException\r
- {\r
- readFully(is, b, 0, b.length); \r
- }\r
- \r
- public static void readFully(InputStream is, byte[] b, int off, int len)\r
- throws IOException\r
- {\r
- while (len > 0) {\r
- int n = is.read(b, off, len);\r
- if (n < 0) throw new EOFException();\r
- off += n;\r
- len -= n;\r
- }\r
- }\r
- \r
- public static void writeFully(byte[] data, File dst) throws IOException {\r
- BinaryFile out = new BinaryFile(dst);\r
- try {\r
- out.write(data);\r
- } finally {\r
- out.flush();\r
- out.close();\r
- }\r
- }\r
- \r
- public static void copyStream(InputStream is, OutputStream out)\r
- throws IOException\r
- {\r
- ReadableByteChannel ic = Channels.newChannel(is);\r
- WritableByteChannel oc = Channels.newChannel(out);\r
- try { \r
- ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);\r
- while (ic.read(buffer) != -1) {\r
- buffer.flip();\r
- oc.write(buffer);\r
- buffer.compact();\r
- }\r
- buffer.flip();\r
- while (buffer.hasRemaining()) {\r
- oc.write(buffer);\r
- }\r
- } finally {\r
-// ic.close();\r
-// oc.close();\r
- }\r
- } \r
- \r
+/*******************************************************************************
+ * Copyright (c) 2010 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.databoard.util;
+
+import java.io.EOFException;
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.Channels;
+import java.nio.channels.ReadableByteChannel;
+import java.nio.channels.WritableByteChannel;
+import java.nio.charset.Charset;
+
+import org.simantics.databoard.util.binary.BinaryFile;
+import org.simantics.databoard.util.binary.BinaryMemory;
+
+public class StreamUtil {
+
+ public static String readString(InputStream is, Charset cs) throws IOException
+ {
+ byte[] data = readFully(is);
+ return new String(data, cs);
+ }
+
+ public static String readString(File file, Charset cs) throws IOException
+ {
+ byte[] data = readFully(file);
+ return new String(data, cs);
+ }
+
+ public static byte[] readFully(InputStream is) throws IOException
+ {
+ BinaryMemory mem = new BinaryMemory( 0 );
+ mem.put(is);
+ byte[] result = new byte[ (int) mem.length() ];
+ mem.position(0L);
+ mem.readFully(result);
+ return result;
+ }
+
+ public static byte[] readFully(File file) throws IOException
+ {
+ BinaryFile b = new BinaryFile(file);
+ try {
+ byte[] bytes = new byte[ (int) b.length() ];
+ b.readFully(bytes);
+ return bytes;
+ } finally {
+ b.close();
+ }
+ }
+
+
+ public static void read(InputStream is, ByteBuffer buf, int bytes)
+ throws IOException
+ {
+ while (bytes>0 & buf.hasRemaining()) {
+ int n = is.read(buf.array(), buf.position(), bytes);
+ if (n < 0) throw new EOFException();
+ buf.position( buf.position() + n );
+ bytes -= n;
+ }
+ }
+
+ public static void readFully(InputStream is, ByteBuffer buf)
+ throws IOException
+ {
+ while (buf.hasRemaining()) {
+ int n = is.read(buf.array(), buf.position(), buf.remaining());
+ if (n < 0) throw new EOFException();
+ buf.position( buf.position() + n );
+ }
+ }
+
+ public static void readFully(InputStream is, byte[] b)
+ throws IOException
+ {
+ readFully(is, b, 0, b.length);
+ }
+
+ public static void readFully(InputStream is, byte[] b, int off, int len)
+ throws IOException
+ {
+ while (len > 0) {
+ int n = is.read(b, off, len);
+ if (n < 0) throw new EOFException();
+ off += n;
+ len -= n;
+ }
+ }
+
+ public static void writeFully(byte[] data, File dst) throws IOException {
+ BinaryFile out = new BinaryFile(dst);
+ try {
+ out.write(data);
+ } finally {
+ out.flush();
+ out.close();
+ }
+ }
+
+ public static void copyStream(InputStream is, OutputStream out)
+ throws IOException
+ {
+ ReadableByteChannel ic = Channels.newChannel(is);
+ WritableByteChannel oc = Channels.newChannel(out);
+ try {
+ ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
+ while (ic.read(buffer) != -1) {
+ buffer.flip();
+ oc.write(buffer);
+ buffer.compact();
+ }
+ buffer.flip();
+ while (buffer.hasRemaining()) {
+ oc.write(buffer);
+ }
+ } finally {
+// ic.close();
+// oc.close();
+ }
+ }
+
}
\ No newline at end of file