]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/util/StreamUtil.java
Fixing several binding-related bugs
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / StreamUtil.java
1 /*******************************************************************************
2  *  Copyright (c) 2010 Association for Decentralized Information Management in
3  *  Industry THTH ry.
4  *  All rights reserved. This program and the accompanying materials
5  *  are made available under the terms of the Eclipse Public License v1.0
6  *  which accompanies this distribution, and is available at
7  *  http://www.eclipse.org/legal/epl-v10.html
8  *
9  *  Contributors:
10  *      VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.databoard.util;
13
14 import java.io.EOFException;
15 import java.io.File;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.OutputStream;
19 import java.nio.ByteBuffer;
20 import java.nio.channels.Channels;
21 import java.nio.channels.ReadableByteChannel;
22 import java.nio.channels.WritableByteChannel;
23 import java.nio.charset.Charset;
24
25 import org.simantics.databoard.util.binary.BinaryFile;
26 import org.simantics.databoard.util.binary.BinaryMemory;
27
28 public class StreamUtil {
29
30         public static String readString(InputStream is, Charset cs) throws IOException 
31         {
32                 byte[] data = readFully(is);
33                 return new String(data, cs);
34         }
35
36         public static String readString(File file, Charset cs) throws IOException 
37         {
38                 byte[] data = readFully(file);
39                 return new String(data, cs);
40         }
41         
42         public static byte[] readFully(InputStream is) throws IOException 
43         {
44                 BinaryMemory mem = new BinaryMemory( 0 );
45                 mem.put(is);
46                 byte[] result = new byte[ (int) mem.length() ];
47                 mem.position(0L);
48                 mem.readFully(result);
49                 return result;
50         }
51         
52         public static byte[] readFully(File file) throws IOException 
53         {
54                 BinaryFile b = new BinaryFile(file);
55                 try {
56                         byte[] bytes = new byte[ (int) b.length() ];
57                         b.readFully(bytes);
58                         return bytes;
59                 } finally {
60                         b.close();
61                 }
62         }
63         
64         
65         public static void read(InputStream is, ByteBuffer buf, int bytes)
66         throws IOException
67         {
68                 while (bytes>0 & buf.hasRemaining()) {
69                         int n = is.read(buf.array(), buf.position(), bytes);
70                         if (n < 0) throw new EOFException();
71                         buf.position( buf.position() + n );
72                         bytes -= n;
73                 }
74         }
75         
76         public static void readFully(InputStream is, ByteBuffer buf)
77         throws IOException
78         {
79                 while (buf.hasRemaining()) {                    
80                         int n = is.read(buf.array(), buf.position(), buf.remaining());
81                         if (n < 0) throw new EOFException();
82                         buf.position( buf.position() + n );
83                 }
84         }
85         
86         public static void readFully(InputStream is, byte[] b)
87         throws IOException
88         {
89                 readFully(is, b, 0, b.length);          
90         }
91         
92         public static void readFully(InputStream is, byte[] b, int off, int len)
93         throws IOException
94         {
95                 while (len > 0) {
96                         int n = is.read(b, off, len);
97                         if (n < 0) throw new EOFException();
98                         off += n;
99                         len -= n;
100                 }
101         }
102         
103         public static void writeFully(byte[] data, File dst) throws IOException {
104                 BinaryFile out = new BinaryFile(dst);
105                 try {
106                         out.write(data);
107                 } finally {
108                         out.flush();
109                         out.close();
110                 }
111         }
112         
113     public static void copyStream(InputStream is, OutputStream out)
114     throws IOException
115     {
116         ReadableByteChannel ic = Channels.newChannel(is);
117         WritableByteChannel oc = Channels.newChannel(out);
118         try {                   
119             ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);
120             while (ic.read(buffer) != -1) {
121                 buffer.flip();
122                 oc.write(buffer);
123                 buffer.compact();
124             }
125             buffer.flip();
126             while (buffer.hasRemaining()) {
127                 oc.write(buffer);
128             }
129         } finally {
130 //              ic.close();
131 //              oc.close();
132         }
133     }   
134         
135 }