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.binary;
14 import java.io.IOException;
\r
15 import java.nio.ByteBuffer;
\r
18 * IWriteable implementation with ByteBuffer as backend
20 * @author Toni Kalajainen (toni.kalajainen@vtt.fi)
22 public class ByteBufferWriteable implements BinaryWriteable {
26 public ByteBufferWriteable(ByteBuffer buf)
29 throw new IllegalArgumentException("null");
34 public void write(int b) {
39 public void writeByte(int b) throws IOException {
\r
44 public void writeBoolean(boolean v) throws IOException {
\r
45 buf.put( (byte) (v ? 1 : 0) );
\r
49 public void writeFully(ByteBuffer src) {
54 public void writeFully(ByteBuffer src, int length) {
56 byte[] array = src.array();
57 buf.put(array, src.arrayOffset() + src.position(), length);
59 for (int i=0; i<length; i++)
65 public void write(byte[] src, int offset, int length) {
66 buf.put(src, offset, length);
70 public void write(byte[] src) {
75 public void writeDouble(double value) {
\r
76 buf.putDouble(value);
\r
80 public void writeFloat(float value) {
81 buf.putFloat(value);
\r
85 public void writeInt(int value) {
90 public void writeLong(long value) {
95 public void writeShort(int value) {
96 buf.putShort((short)value);
100 public void writeChar(int value) {
\r
101 buf.putChar((char) value);
\r
105 public void writeBytes(String s) throws IOException {
\r
106 int len = s.length();
\r
107 for (int i = 0 ; i < len ; i++) {
\r
108 buf.put((byte)s.charAt(i));
\r
113 public void writeChars(String s) throws IOException {
\r
114 int len = s.length();
\r
115 for (int i = 0 ; i < len ; i++) {
\r
116 char v = s.charAt(i);
\r
122 public void writeUTF(String s) throws IOException {
\r
123 int len = UTF8.getModifiedUTF8EncodingByteLength(s);
\r
125 UTF8.writeModifiedUTF(this, s);
\r
129 public void flush() {
132 public long position() throws IOException {
133 return buf.position();
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);