]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/util/StreamUtil.java
Re-implement URIStringUtils escape and unescape using Unicode
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / StreamUtil.java
1 /*******************************************************************************\r
2  *  Copyright (c) 2010 Association for Decentralized Information Management in\r
3  *  Industry THTH ry.\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
8  *\r
9  *  Contributors:\r
10  *      VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.databoard.util;\r
13 \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
24 \r
25 import org.simantics.databoard.util.binary.BinaryFile;\r
26 import org.simantics.databoard.util.binary.BinaryMemory;\r
27 \r
28 public class StreamUtil {\r
29 \r
30         public static String readString(InputStream is, Charset cs) throws IOException \r
31         {\r
32                 byte[] data = readFully(is);\r
33                 return new String(data, cs);\r
34         }\r
35 \r
36         public static String readString(File file, Charset cs) throws IOException \r
37         {\r
38                 byte[] data = readFully(file);\r
39                 return new String(data, cs);\r
40         }\r
41         \r
42         public static byte[] readFully(InputStream is) throws IOException \r
43         {\r
44                 BinaryMemory mem = new BinaryMemory( 0 );\r
45                 mem.put(is);\r
46                 byte[] result = new byte[ (int) mem.length() ];\r
47                 mem.position(0L);\r
48                 mem.readFully(result);\r
49                 return result;\r
50         }\r
51         \r
52         public static byte[] readFully(File file) throws IOException \r
53         {\r
54                 BinaryFile b = new BinaryFile(file);\r
55                 try {\r
56                         byte[] bytes = new byte[ (int) b.length() ];\r
57                         b.readFully(bytes);\r
58                         return bytes;\r
59                 } finally {\r
60                         b.close();\r
61                 }\r
62         }\r
63         \r
64         \r
65         public static void read(InputStream is, ByteBuffer buf, int bytes)\r
66         throws IOException\r
67         {\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
72                         bytes -= n;\r
73                 }\r
74         }\r
75         \r
76         public static void readFully(InputStream is, ByteBuffer buf)\r
77         throws IOException\r
78         {\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
83                 }\r
84         }\r
85         \r
86         public static void readFully(InputStream is, byte[] b)\r
87         throws IOException\r
88         {\r
89                 readFully(is, b, 0, b.length);          \r
90         }\r
91         \r
92         public static void readFully(InputStream is, byte[] b, int off, int len)\r
93         throws IOException\r
94         {\r
95                 while (len > 0) {\r
96                         int n = is.read(b, off, len);\r
97                         if (n < 0) throw new EOFException();\r
98                         off += n;\r
99                         len -= n;\r
100                 }\r
101         }\r
102         \r
103         public static void writeFully(byte[] data, File dst) throws IOException {\r
104                 BinaryFile out = new BinaryFile(dst);\r
105                 try {\r
106                         out.write(data);\r
107                 } finally {\r
108                         out.flush();\r
109                         out.close();\r
110                 }\r
111         }\r
112         \r
113     public static void copyStream(InputStream is, OutputStream out)\r
114     throws IOException\r
115     {\r
116         ReadableByteChannel ic = Channels.newChannel(is);\r
117         WritableByteChannel oc = Channels.newChannel(out);\r
118         try {                   \r
119             ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024);\r
120             while (ic.read(buffer) != -1) {\r
121                 buffer.flip();\r
122                 oc.write(buffer);\r
123                 buffer.compact();\r
124             }\r
125             buffer.flip();\r
126             while (buffer.hasRemaining()) {\r
127                 oc.write(buffer);\r
128             }\r
129         } finally {\r
130 //              ic.close();\r
131 //              oc.close();\r
132         }\r
133     }   \r
134         \r
135 }