]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.databoard/src/org/simantics/databoard/util/binary/ByteBufferWriteable.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.databoard / src / org / simantics / databoard / util / binary / ByteBufferWriteable.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.nio.ByteBuffer;
16
17 /**
18  * IWriteable implementation with ByteBuffer as backend
19  * 
20  * @author Toni Kalajainen (toni.kalajainen@vtt.fi)
21  */
22 public class ByteBufferWriteable implements BinaryWriteable {
23
24         ByteBuffer buf;
25         
26         public ByteBufferWriteable(ByteBuffer buf)
27         {
28                 if (buf == null)
29                         throw new IllegalArgumentException("null");
30                 this.buf = buf;
31         }
32
33         @Override
34         public void write(int b) {
35                 buf.put((byte) b);
36         }
37
38         @Override
39         public void writeByte(int b) throws IOException {
40                 buf.put((byte) b);
41         }
42         
43         @Override
44         public void writeBoolean(boolean v) throws IOException {
45                 buf.put( (byte) (v ? 1 : 0) );
46         }
47         
48         @Override
49         public void writeFully(ByteBuffer src) {
50                 buf.put(src);
51         }
52
53         @Override
54         public void writeFully(ByteBuffer src, int length) {
55                 if (src.hasArray()) {
56                         byte[] array = src.array();
57                         buf.put(array, src.arrayOffset() + src.position(), length);
58                 } else {
59                         for (int i=0; i<length; i++)
60                                 buf.put(src.get());
61                 }
62         }
63
64         @Override
65         public void write(byte[] src, int offset, int length) {
66                 buf.put(src, offset, length);
67         }
68
69         @Override
70         public void write(byte[] src) {
71                 buf.put(src);
72         }
73
74         @Override
75         public void writeDouble(double value) {
76                 buf.putDouble(value);
77         }
78
79         @Override
80         public void writeFloat(float value) {
81                 buf.putFloat(value);
82         }
83
84         @Override
85         public void writeInt(int value) {
86                 buf.putInt(value);
87         }
88
89         @Override
90         public void writeLong(long value) {
91                 buf.putLong(value);
92         }
93
94         @Override
95         public void writeShort(int value) {
96                 buf.putShort((short)value);
97         }
98         
99         @Override
100         public void writeChar(int value) {
101                 buf.putChar((char) value);
102         }       
103
104         @Override
105         public void writeBytes(String s) throws IOException {
106                 int len = s.length();
107                 for (int i = 0 ; i < len ; i++) {
108                     buf.put((byte)s.charAt(i));
109                 }
110         }
111         
112         @Override
113         public void writeChars(String s) throws IOException {
114         int len = s.length();
115         for (int i = 0 ; i < len ; i++) {
116             char v = s.charAt(i);
117             buf.putChar(v);
118         }
119         }
120         
121         @Override
122         public void writeUTF(String s) throws IOException {
123                 int len = UTF8.getModifiedUTF8EncodingByteLength(s);
124                 writeShort(len);
125                 UTF8.writeModifiedUTF(this, s);
126         }
127
128         @Override
129         public void flush() {
130         }
131         
132         public long position() throws IOException {
133                 return buf.position();
134         }
135         
136         public void position(long newPosition) throws IOException {
137                 if (newPosition>=Integer.MAX_VALUE || newPosition<0) throw new IllegalArgumentException("index out of range");
138                 buf.position((int) newPosition);                
139         }
140         
141 }
142