1 /*******************************************************************************
2 * Copyright (c) 2010 Association for Decentralized Information Management in
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.databoard.util.binary;
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import java.nio.ByteBuffer;
18 public class OutputStreamWriteable implements BinaryWriteable {
22 public OutputStreamWriteable(OutputStream out)
24 if (out==null) throw new IllegalArgumentException("null arg");
28 public OutputStream getStream()
40 public void write(int b) throws IOException {
45 public void writeByte(int b) throws IOException {
49 public void writeBoolean(boolean v) throws IOException {
54 public void writeFully(ByteBuffer src) throws IOException {
56 byte array[] = src.array();
57 write(array, src.position(), src.remaining());
58 src.position(src.limit());
60 for (;src.hasRemaining();)
65 public void writeFully(ByteBuffer src, int length) throws IOException {
67 byte array[] = src.array();
68 write(array, src.position(), length);
71 for (int i=0; i<length; i++)
77 public void write(byte[] src, int offset, int length) throws IOException {
78 out.write(src, offset, length);
82 public void write(byte[] src) throws IOException {
87 public void writeDouble(double value) throws IOException {
88 writeLong(Double.doubleToLongBits(value));
92 public void writeFloat(float value) throws IOException {
93 writeInt(Float.floatToIntBits(value));
97 public void writeInt(int value) throws IOException {
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));
117 public void writeShort(int value) throws IOException {
123 public void writeChar(int value) throws IOException {
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));
137 public void writeChars(String s) throws IOException {
138 int len = s.length();
139 for (int i = 0 ; i < len ; i++) {
141 _put((v >>> 8) & 0xFF);
142 _put((v >>> 0) & 0xFF);
147 public void writeUTF(String s) throws IOException {
148 int len = UTF8.getModifiedUTF8EncodingByteLength(s);
150 UTF8.writeModifiedUTF(this, s);
154 public void flush() throws IOException {