1 /*******************************************************************************
\r
2 * Copyright (c) 2010 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.databoard.util;
\r
14 import java.io.EOFException;
\r
15 import java.io.File;
\r
16 import java.io.IOException;
\r
17 import java.io.InputStream;
\r
18 import java.io.OutputStream;
\r
19 import java.nio.ByteBuffer;
\r
20 import java.nio.channels.Channels;
\r
21 import java.nio.channels.ReadableByteChannel;
\r
22 import java.nio.channels.WritableByteChannel;
\r
23 import java.nio.charset.Charset;
\r
25 import org.simantics.databoard.util.binary.BinaryFile;
\r
26 import org.simantics.databoard.util.binary.BinaryMemory;
\r
28 public class StreamUtil {
\r
30 public static String readString(InputStream is, Charset cs) throws IOException
\r
32 byte[] data = readFully(is);
\r
33 return new String(data, cs);
\r
36 public static String readString(File file, Charset cs) throws IOException
\r
38 byte[] data = readFully(file);
\r
39 return new String(data, cs);
\r
42 public static byte[] readFully(InputStream is) throws IOException
\r
44 BinaryMemory mem = new BinaryMemory( 0 );
\r
46 byte[] result = new byte[ (int) mem.length() ];
\r
48 mem.readFully(result);
\r
52 public static byte[] readFully(File file) throws IOException
\r
54 BinaryFile b = new BinaryFile(file);
\r
56 byte[] bytes = new byte[ (int) b.length() ];
\r
65 public static void read(InputStream is, ByteBuffer buf, int bytes)
\r
68 while (bytes>0 & buf.hasRemaining()) {
\r
69 int n = is.read(buf.array(), buf.position(), bytes);
\r
70 if (n < 0) throw new EOFException();
\r
71 buf.position( buf.position() + n );
\r
76 public static void readFully(InputStream is, ByteBuffer buf)
\r
79 while (buf.hasRemaining()) {
\r
80 int n = is.read(buf.array(), buf.position(), buf.remaining());
\r
81 if (n < 0) throw new EOFException();
\r
82 buf.position( buf.position() + n );
\r
86 public static void readFully(InputStream is, byte[] b)
\r
89 readFully(is, b, 0, b.length);
\r
92 public static void readFully(InputStream is, byte[] b, int off, int len)
\r
96 int n = is.read(b, off, len);
\r
97 if (n < 0) throw new EOFException();
\r
103 public static void writeFully(byte[] data, File dst) throws IOException {
\r
104 BinaryFile out = new BinaryFile(dst);
\r
113 public static void copyStream(InputStream is, OutputStream out)
\r
116 ReadableByteChannel ic = Channels.newChannel(is);
\r
117 WritableByteChannel oc = Channels.newChannel(out);
\r
119 ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
\r
120 while (ic.read(buffer) != -1) {
\r
126 while (buffer.hasRemaining()) {
\r