]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.databoard/src/org/simantics/databoard/util/StreamUtil.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / StreamUtil.java
diff --git a/bundles/org.simantics.databoard/src/org/simantics/databoard/util/StreamUtil.java b/bundles/org.simantics.databoard/src/org/simantics/databoard/util/StreamUtil.java
new file mode 100644 (file)
index 0000000..f2b134b
--- /dev/null
@@ -0,0 +1,135 @@
+/*******************************************************************************\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
+}
\ No newline at end of file