--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2010 Association for Decentralized Information Management in\r
+ * Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.databoard.util.binary;
+
+import java.io.IOException;\r
+import java.nio.ByteBuffer;\r
+
+/**
+ * IWriteable implementation with ByteBuffer as backend
+ *
+ * @author Toni Kalajainen (toni.kalajainen@vtt.fi)
+ */
+public class ByteBufferWriteable implements BinaryWriteable {
+
+ ByteBuffer buf;
+
+ public ByteBufferWriteable(ByteBuffer buf)
+ {
+ if (buf == null)
+ throw new IllegalArgumentException("null");
+ this.buf = buf;
+ }
+
+ @Override
+ public void write(int b) {
+ buf.put((byte) b);
+ }
+\r
+ @Override\r
+ public void writeByte(int b) throws IOException {\r
+ buf.put((byte) b);\r
+ }\r
+ \r
+ @Override\r
+ public void writeBoolean(boolean v) throws IOException {\r
+ buf.put( (byte) (v ? 1 : 0) );\r
+ }\r
+
+ @Override
+ public void writeFully(ByteBuffer src) {
+ buf.put(src);
+ }
+
+ @Override
+ public void writeFully(ByteBuffer src, int length) {
+ if (src.hasArray()) {
+ byte[] array = src.array();
+ buf.put(array, src.arrayOffset() + src.position(), length);
+ } else {
+ for (int i=0; i<length; i++)
+ buf.put(src.get());
+ }
+ }
+
+ @Override
+ public void write(byte[] src, int offset, int length) {
+ buf.put(src, offset, length);
+ }
+
+ @Override
+ public void write(byte[] src) {
+ buf.put(src);
+ }
+
+ @Override
+ public void writeDouble(double value) {\r
+ buf.putDouble(value);\r
+ }
+
+ @Override
+ public void writeFloat(float value) {
+ buf.putFloat(value);\r
+ }
+
+ @Override
+ public void writeInt(int value) {
+ buf.putInt(value);
+ }
+
+ @Override
+ public void writeLong(long value) {
+ buf.putLong(value);
+ }
+
+ @Override
+ public void writeShort(int value) {
+ buf.putShort((short)value);
+ }\r
+ \r
+ @Override\r
+ public void writeChar(int value) {\r
+ buf.putChar((char) value);\r
+ }
+\r
+ @Override\r
+ public void writeBytes(String s) throws IOException {\r
+ int len = s.length();\r
+ for (int i = 0 ; i < len ; i++) {\r
+ buf.put((byte)s.charAt(i));\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public void writeChars(String s) throws IOException {\r
+ int len = s.length();\r
+ for (int i = 0 ; i < len ; i++) {\r
+ char v = s.charAt(i);\r
+ buf.putChar(v);\r
+ }\r
+ }\r
+ \r
+ @Override\r
+ public void writeUTF(String s) throws IOException {\r
+ int len = UTF8.getModifiedUTF8EncodingByteLength(s);\r
+ writeShort(len);\r
+ UTF8.writeModifiedUTF(this, s);\r
+ }
+
+ @Override
+ public void flush() {
+ }
+
+ public long position() throws IOException {
+ return buf.position();
+ }
+
+ public void position(long newPosition) throws IOException {
+ if (newPosition>=Integer.MAX_VALUE || newPosition<0) throw new IllegalArgumentException("index out of range");
+ buf.position((int) newPosition);
+ }
+
+}
+