X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.databoard%2Fsrc%2Forg%2Fsimantics%2Fdataboard%2Futil%2FStreamUtil.java;h=99ab799c9cbeeeeff92772f2be5293b54e0665f9;hb=694c553a3c402b9397ff99e904b4f99b77b51df1;hp=f2b134bfe0b3a6e487e111bfdddfdf3ad7159632;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git 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 index f2b134bfe..99ab799c9 100644 --- a/bundles/org.simantics.databoard/src/org/simantics/databoard/util/StreamUtil.java +++ b/bundles/org.simantics.databoard/src/org/simantics/databoard/util/StreamUtil.java @@ -1,135 +1,135 @@ -/******************************************************************************* - * 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(); - } - } - +/******************************************************************************* + * 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