001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.commons.encoding;
021
022import java.io.DataOutputStream;
023import java.io.IOException;
024import java.io.OutputStream;
025import java.io.Serializable;
026
027public class DataOutputStreamExtended implements DataOutputExtended {
028
029    private final DataOutputStream dataOutputStream;
030
031    public DataOutputStreamExtended(final OutputStream output) {
032        dataOutputStream = new DataOutputStream(output);
033    }
034
035    @Override
036    public DataOutputStream getDataOutputStream() {
037        return dataOutputStream;
038    }
039
040    // ////////////////////////////////////////
041    // Boolean, Char
042    // ////////////////////////////////////////
043
044    @Override
045    public void writeBoolean(final boolean value) throws IOException {
046        FieldType.BOOLEAN.write(this, value);
047    }
048
049    @Override
050    public void writeBooleans(final boolean[] value) throws IOException {
051        FieldType.BOOLEAN_ARRAY.write(this, value);
052    }
053
054    @Override
055    public void writeChar(final int value) throws IOException {
056        FieldType.CHAR.write(this, (char) value);
057    }
058
059    @Override
060    public void writeChars(final char[] value) throws IOException {
061        FieldType.CHAR_ARRAY.write(this, value);
062    }
063
064    
065    // ////////////////////////////////////////
066    // Integral Numbers
067    // ////////////////////////////////////////
068
069    @Override
070    public void write(final int value) throws IOException {
071        writeByte((byte) value);
072    }
073
074    @Override
075    public void writeByte(final int value) throws IOException {
076        FieldType.BYTE.write(this, (byte) value);
077    }
078
079    @Override
080    public void write(final byte[] value) throws IOException {
081        writeBytes(value);
082    }
083
084    @Override
085    public void writeBytes(final byte[] value) throws IOException {
086        FieldType.BYTE_ARRAY.write(this, value);
087    }
088
089    @Override
090    public void writeShort(final int value) throws IOException {
091        FieldType.SHORT.write(this, (short) value);
092    }
093
094    @Override
095    public void writeShorts(final short[] value) throws IOException {
096        FieldType.SHORT_ARRAY.write(this, value);
097    }
098
099    @Override
100    public void writeInt(final int value) throws IOException {
101        FieldType.INTEGER.write(this, value);
102    }
103
104    @Override
105    public void writeInts(final int[] value) throws IOException {
106        FieldType.INTEGER_ARRAY.write(this, value);
107    }
108
109    @Override
110    public void writeLong(final long value) throws IOException {
111        FieldType.LONG.write(this, value);
112    }
113
114    @Override
115    public void writeLongs(final long[] value) throws IOException {
116        FieldType.LONG_ARRAY.write(this, value);
117    }
118
119    // ////////////////////////////////////////
120    // Floating Point Numbers
121    // ////////////////////////////////////////
122
123    @Override
124    public void writeFloat(final float value) throws IOException {
125        FieldType.FLOAT.write(this, value);
126    }
127
128    @Override
129    public void writeFloats(final float[] value) throws IOException {
130        FieldType.FLOAT_ARRAY.write(this, value);
131    }
132
133    @Override
134    public void writeDouble(final double value) throws IOException {
135        FieldType.DOUBLE.write(this, value);
136    }
137
138    @Override
139    public void writeDoubles(final double[] value) throws IOException {
140        FieldType.DOUBLE_ARRAY.write(this, value);
141    }
142
143    // ////////////////////////////////////////
144    // Strings
145    // ////////////////////////////////////////
146
147    @Override
148    public void writeUTF(final String value) throws IOException {
149        FieldType.STRING.write(this, value);
150    }
151
152    @Override
153    public void writeUTFs(final String[] value) throws IOException {
154        FieldType.STRING_ARRAY.write(this, value);
155    }
156
157    // ////////////////////////////////////////
158    // Encodable and Serializable
159    // ////////////////////////////////////////
160
161    @Override
162    public void writeEncodable(final Object encodable) throws IOException {
163        FieldType.ENCODABLE.write(this, (Encodable) encodable);
164    }
165
166    @Override
167    public void writeEncodables(final Object[] objects) throws IOException {
168        Encodable[] encodables;
169        if (objects == null) {
170            encodables = null;
171        } else {
172            encodables = new Encodable[objects.length];
173            for (int i = 0; i < encodables.length; i++) {
174                encodables[i] = (Encodable) objects[i];
175            }
176        }
177        FieldType.ENCODABLE_ARRAY.write(this, encodables);
178    }
179
180    @Override
181    public void writeSerializable(final Object serializable) throws IOException {
182        FieldType.SERIALIZABLE.write(this, (Serializable) serializable);
183    }
184
185    @Override
186    public void writeSerializables(final Object[] objects) throws IOException {
187        Serializable[] serializeables;
188        if (objects == null) {
189            serializeables = null;
190        } else {
191            serializeables = new Serializable[objects.length];
192            for (int i = 0; i < serializeables.length; i++) {
193                serializeables[i] = (Serializable) objects[i];
194            }
195        }
196        FieldType.SERIALIZABLE_ARRAY.write(this, serializeables);
197    }
198
199    // ////////////////////////////////////////
200    // Other
201    // ////////////////////////////////////////
202
203    @Override
204    public void write(final byte[] b, final int off, final int len) throws IOException {
205        dataOutputStream.write(b, off, len);
206    }
207
208    @Override
209    public void writeBytes(final String str) throws IOException {
210        dataOutputStream.writeBytes(str);
211    }
212
213    @Override
214    public void writeChars(final String str) throws IOException {
215        dataOutputStream.writeChars(str);
216    }
217
218    // ////////////////////////////////////////
219    // Flushable
220    // ////////////////////////////////////////
221
222    @Override
223    public void flush() throws IOException {
224        dataOutputStream.flush();
225    }
226
227}