Imported GNU Classpath 0.90
[official-gcc.git] / libjava / classpath / javax / imageio / stream / ImageOutputStreamImpl.java
blob1179fed7dcdde5c2794c0613f3bd1535c844a3fd
1 /* ImageOutputStream.java --
2 Copyright (C) 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.imageio.stream;
41 import gnu.classpath.NotImplementedException;
43 import java.io.IOException;
44 import java.io.UTFDataFormatException;
45 import java.nio.ByteOrder;
47 /**
48 * @author Michael Koch (konqueror@gmx.de)
50 public abstract class ImageOutputStreamImpl extends ImageInputStreamImpl
51 implements ImageOutputStream
53 public ImageOutputStreamImpl()
55 // Do nothing here.
58 protected final void flushBits()
59 throws IOException, NotImplementedException
61 // FIXME: Implement me.
62 throw new Error("not implemented");
65 public void write(byte[] data)
66 throws IOException
68 write(data, 0, data.length);
71 public abstract void write(byte[] data, int offset, int len)
72 throws IOException;
74 public abstract void write(int value)
75 throws IOException;
77 public void writeBit(int bit)
78 throws IOException, NotImplementedException
80 // FIXME: Implement me.
81 throw new Error("not implemented");
84 public void writeBits(long bits, int numBits)
85 throws IOException, NotImplementedException
87 // FIXME: Implement me.
88 throw new Error("not implemented");
91 public void writeBoolean(boolean value)
92 throws IOException
94 writeByte(value ? 1 : 0);
97 public void writeByte(int value)
98 throws IOException
100 write(value & 0xff);
103 public void writeBytes(String data)
104 throws IOException
106 // This is bogus, but it is how the method is specified.
107 // Sun ought to deprecate this method.
108 int len = data.length();
109 for (int i = 0; i < len; ++i)
110 writeByte(data.charAt(i));
113 public void writeChar(int value)
114 throws IOException
116 writeShort(value);
119 public void writeChars(char[] data, int offset, int len)
120 throws IOException
122 for(int i = 0; i < len; ++len)
123 writeChar(data[offset + i]);
126 public void writeChars(String data)
127 throws IOException
129 int len = data.length();
130 for (int i = 0; i < len; ++i)
131 writeChar(data.charAt(i));
134 public void writeDouble(double value)
135 throws IOException
137 writeLong(Double.doubleToLongBits(value));
140 public void writeDoubles(double[] data, int offset, int len)
141 throws IOException
143 for(int i = 0; i < len; ++len)
144 writeDouble(data[offset + i]);
147 public void writeFloat(float value)
148 throws IOException
150 writeInt(Float.floatToIntBits(value));
153 public void writeFloats(float[] data, int offset, int len)
154 throws IOException
156 for(int i = 0; i < len; ++len)
157 writeFloat(data[offset + i]);
160 public void writeInt(int value)
161 throws IOException
163 if (getByteOrder() == ByteOrder.LITTLE_ENDIAN)
165 buffer[0] = ((byte) value);
166 buffer[1] = ((byte) (value >> 8));
167 buffer[2] = ((byte) (value >> 16));
168 buffer[3] = ((byte) (value >> 24));
170 else
172 buffer[0] = ((byte) (value >> 24));
173 buffer[1] = ((byte) (value >> 16));
174 buffer[2] = ((byte) (value >> 8));
175 buffer[3] = ((byte) value);
178 write(buffer, 0, 4);
181 public void writeInts(int[] data, int offset, int len)
182 throws IOException
184 for(int i = 0; i < len; ++len)
185 writeInt(data[offset + i]);
188 public void writeLong(long value)
189 throws IOException
191 if (getByteOrder() == ByteOrder.LITTLE_ENDIAN)
193 buffer[0] = ((byte) value);
194 buffer[1] = ((byte) (value >> 8));
195 buffer[2] = ((byte) (value >> 16));
196 buffer[3] = ((byte) (value >> 24));
197 buffer[4] = ((byte) (value >> 32));
198 buffer[5] = ((byte) (value >> 40));
199 buffer[6] = ((byte) (value >> 48));
200 buffer[7] = ((byte) (value >> 56));
202 else
204 buffer[0] = ((byte) (value >> 56));
205 buffer[1] = ((byte) (value >> 48));
206 buffer[2] = ((byte) (value >> 40));
207 buffer[3] = ((byte) (value >> 32));
208 buffer[4] = ((byte) (value >> 24));
209 buffer[5] = ((byte) (value >> 16));
210 buffer[6] = ((byte) (value >> 8));
211 buffer[7] = ((byte) value);
214 write(buffer, 0, 8);
217 public void writeLongs(long[] data, int offset, int len)
218 throws IOException
220 for(int i = 0; i < len; ++len)
221 writeLong(data[offset + i]);
224 public void writeShort(int value)
225 throws IOException
227 if (getByteOrder() == ByteOrder.LITTLE_ENDIAN)
229 buffer[0] = ((byte) value);
230 buffer[1] = ((byte) (value >> 8));
232 else
234 buffer[0] = ((byte) (value >> 8));
235 buffer[1] = ((byte) value);
238 write(buffer, 0, 2);
241 public void writeShorts(short[] data, int offset, int len)
242 throws IOException
244 for(int i = 0; i < len; ++len)
245 writeShort(data[offset + i]);
248 public void writeUTF(String value)
249 throws IOException
251 // NOTE: this code comes directly from DataOutputStream.
252 int len = value.length();
253 int sum = 0;
255 for (int i = 0; i < len && sum <= 65535; ++i)
257 char c = value.charAt(i);
258 if (c >= '\u0001' && c <= '\u007f')
259 sum += 1;
260 else if (c == '\u0000' || (c >= '\u0080' && c <= '\u07ff'))
261 sum += 2;
262 else
263 sum += 3;
266 if (sum > 65535)
267 throw new UTFDataFormatException ();
269 int pos = 0;
270 byte[] buf = new byte[sum];
272 for (int i = 0; i < len; ++i)
274 char c = value.charAt(i);
275 if (c >= '\u0001' && c <= '\u007f')
276 buf[pos++] = (byte) c;
277 else if (c == '\u0000' || (c >= '\u0080' && c <= '\u07ff'))
279 buf[pos++] = (byte) (0xc0 | (0x1f & (c >> 6)));
280 buf[pos++] = (byte) (0x80 | (0x3f & c));
282 else
284 // JSL says the first byte should be or'd with 0xc0, but
285 // that is a typo. Unicode says 0xe0, and that is what is
286 // consistent with DataInputStream.
287 buf[pos++] = (byte) (0xe0 | (0x0f & (c >> 12)));
288 buf[pos++] = (byte) (0x80 | (0x3f & (c >> 6)));
289 buf[pos++] = (byte) (0x80 | (0x3f & c));
293 writeShort (sum);
294 write(buf, 0, sum);