]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/OutputStreamWriteable.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / binary / OutputStreamWriteable.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.binary;
13
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import java.nio.ByteBuffer;
17
18 public class OutputStreamWriteable implements BinaryWriteable {
19
20         OutputStream out;
21                 
22         public OutputStreamWriteable(OutputStream out)
23         {
24                 if (out==null) throw new IllegalArgumentException("null arg");
25                 this.out = out;
26         }
27         
28         public OutputStream getStream()
29         {
30                 return out;
31         }
32
33         void _put(int value)
34         throws IOException
35         {
36                 out.write(value);
37         }
38         
39         @Override
40         public void write(int b) throws IOException {
41                 _put(b);
42         }
43         
44         @Override
45         public void writeByte(int b) throws IOException {
46                 _put(b);
47         }       
48         @Override
49         public void writeBoolean(boolean v) throws IOException {
50                 _put( v ? 1 : 0);
51         }
52
53         @Override
54         public void writeFully(ByteBuffer src) throws IOException {
55                 if (src.hasArray()) {
56                         byte array[] = src.array();
57                         write(array, src.position(), src.remaining());
58                         src.position(src.limit()); 
59                 } else 
60                         for (;src.hasRemaining();)
61                                 _put(src.get());
62         }
63
64         @Override
65         public void writeFully(ByteBuffer src, int length) throws IOException {         
66                 if (src.hasArray()) {
67                         byte array[] = src.array();
68                         write(array, src.position(), length);
69                         src.position(length); 
70                 } else {
71                         for (int i=0; i<length; i++)
72                                 _put(src.get());
73                 }
74         }
75
76         @Override
77         public void write(byte[] src, int offset, int length) throws IOException {
78                 out.write(src, offset, length);
79         }
80
81         @Override
82         public void write(byte[] src) throws IOException {
83                 out.write(src);
84         }
85
86         @Override
87         public void writeDouble(double value) throws IOException {
88                 writeLong(Double.doubleToLongBits(value));
89         }
90
91         @Override
92         public void writeFloat(float value) throws IOException {
93                 writeInt(Float.floatToIntBits(value));
94         }
95
96         @Override
97         public void writeInt(int value) throws IOException {
98                 _put(value >> 24);
99                 _put(value >> 16);
100                 _put(value >> 8);
101                 _put(value);
102         }
103
104         @Override
105         public void writeLong(long value) throws IOException {
106                 _put((int) (value >> 56));
107                 _put((int) (value >> 48));
108                 _put((int) (value >> 40));
109                 _put((int) (value >> 32));
110                 _put((int) (value >> 24));
111                 _put((int) (value >> 16));
112                 _put((int) (value >> 8));
113                 _put((int) (value));
114         }
115
116         @Override
117         public void writeShort(int value) throws IOException {
118                 _put(value >> 8);
119                 _put(value);
120         }
121         
122         @Override
123         public void writeChar(int value) throws IOException {
124                 _put(value >> 8);
125                 _put(value);
126         }       
127         
128         @Override
129         public void writeBytes(String s) throws IOException {
130                 int len = s.length();
131                 for (int i = 0 ; i < len ; i++) {
132                     _put((byte)s.charAt(i));
133                 }
134         }
135         
136         @Override
137         public void writeChars(String s) throws IOException {
138         int len = s.length();
139         for (int i = 0 ; i < len ; i++) {
140             int v = s.charAt(i);
141             _put((v >>> 8) & 0xFF); 
142             _put((v >>> 0) & 0xFF); 
143         }
144         }
145         
146         @Override
147         public void writeUTF(String s) throws IOException {
148                 int len = UTF8.getModifiedUTF8EncodingByteLength(s);
149                 writeShort(len);
150                 UTF8.writeModifiedUTF(this, s);
151         }
152         
153         @Override
154         public void flush() throws IOException {
155                 out.flush();
156         }
157
158 }
159
160