]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.compressions/src/org/simantics/compressions/impl/Channels.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.compressions / src / org / simantics / compressions / impl / Channels.java
diff --git a/bundles/org.simantics.compressions/src/org/simantics/compressions/impl/Channels.java b/bundles/org.simantics.compressions/src/org/simantics/compressions/impl/Channels.java
new file mode 100644 (file)
index 0000000..41c8ec8
--- /dev/null
@@ -0,0 +1,50 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management\r
+ * in 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.compressions.impl;\r
+\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.nio.ByteBuffer;\r
+import java.nio.channels.ReadableByteChannel;\r
+\r
+/**\r
+ * @author Tuukka Lehtonen\r
+ */\r
+public class Channels {\r
+\r
+    public static int read(ReadableByteChannel channel, ByteBuffer intoBuffer, int expectedBytes) throws IOException {\r
+        int read = 0;\r
+        while (read < expectedBytes) {\r
+            int ret = channel.read(intoBuffer);\r
+            if (ret < 0)\r
+                return read; // End-Of-Stream\r
+            read += ret;\r
+        }\r
+        return read;\r
+    }\r
+\r
+    public static int read(ReadableByteChannel channel, ByteBuffer intoBuffer) throws IOException {\r
+        return read(channel, intoBuffer, intoBuffer.limit());\r
+    }\r
+\r
+    public static int readStream(InputStream stream, byte[] array, int expectedBytes) throws IOException {\r
+        int read = 0;\r
+        while (read < expectedBytes) {\r
+            int ret = stream.read(array, read, expectedBytes - read);\r
+            if (ret < 0)\r
+                return read; // End-Of-Stream\r
+            read += ret;\r
+        }\r
+        return read;\r
+    }\r
+\r
+}
\ No newline at end of file