001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Rustem V. Rafikov
019: * @version $Revision: 1.3 $
020: */package javax.imageio.stream;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.DataOutputStream;
024: import java.io.IOException;
025: import java.nio.ByteOrder;
026:
027: import org.apache.harmony.luni.util.NotImplementedException;
028:
029: public abstract class ImageOutputStreamImpl extends
030: ImageInputStreamImpl implements ImageOutputStream {
031:
032: private final byte[] buff = new byte[8];
033:
034: public ImageOutputStreamImpl() {
035: }
036:
037: public abstract void write(int b) throws IOException;
038:
039: public void write(byte[] b) throws IOException {
040: write(b, 0, b.length);
041: }
042:
043: public abstract void write(byte[] b, int off, int len)
044: throws IOException;
045:
046: public void writeBoolean(boolean v) throws IOException {
047: write(v ? 1 : 0);
048: }
049:
050: public void writeByte(int v) throws IOException {
051: write(v);
052: }
053:
054: public void writeShort(int v) throws IOException {
055: if (byteOrder == ByteOrder.BIG_ENDIAN) {
056: buff[0] = (byte) (v >> 8);
057: buff[1] = (byte) v;
058: } else {
059: buff[1] = (byte) (v >> 8);
060: buff[0] = (byte) v;
061: }
062:
063: write(buff, 0, 2);
064: }
065:
066: public void writeChar(int v) throws IOException {
067: writeShort(v);
068: }
069:
070: public void writeInt(int v) throws IOException {
071: if (byteOrder == ByteOrder.BIG_ENDIAN) {
072: buff[0] = (byte) (v >> 24);
073: buff[1] = (byte) (v >> 16);
074: buff[2] = (byte) (v >> 8);
075: buff[3] = (byte) v;
076: } else {
077: buff[3] = (byte) (v >> 24);
078: buff[2] = (byte) (v >> 16);
079: buff[1] = (byte) (v >> 8);
080: buff[0] = (byte) v;
081: }
082:
083: write(buff, 0, 4);
084: }
085:
086: public void writeLong(long v) throws IOException {
087: if (byteOrder == ByteOrder.BIG_ENDIAN) {
088: buff[0] = (byte) (v >> 56);
089: buff[1] = (byte) (v >> 48);
090: buff[2] = (byte) (v >> 40);
091: buff[3] = (byte) (v >> 32);
092: buff[4] = (byte) (v >> 24);
093: buff[5] = (byte) (v >> 16);
094: buff[6] = (byte) (v >> 8);
095: buff[7] = (byte) (v);
096: } else {
097: buff[7] = (byte) (v >> 56);
098: buff[6] = (byte) (v >> 48);
099: buff[5] = (byte) (v >> 40);
100: buff[4] = (byte) (v >> 32);
101: buff[3] = (byte) (v >> 24);
102: buff[2] = (byte) (v >> 16);
103: buff[1] = (byte) (v >> 8);
104: buff[0] = (byte) (v);
105: }
106:
107: write(buff, 0, 8);
108: }
109:
110: public void writeFloat(float v) throws IOException {
111: writeInt(Float.floatToIntBits(v));
112: }
113:
114: public void writeDouble(double v) throws IOException {
115: writeLong(Double.doubleToLongBits(v));
116: }
117:
118: public void writeBytes(String s) throws IOException {
119: write(s.getBytes());
120: }
121:
122: public void writeChars(String s) throws IOException {
123: char[] chs = s.toCharArray();
124: writeChars(chs, 0, chs.length);
125: }
126:
127: public void writeUTF(String s) throws IOException {
128: ByteArrayOutputStream baos = new ByteArrayOutputStream();
129:
130: new DataOutputStream(baos).writeUTF(s);
131: write(baos.toByteArray(), 0, baos.size());
132: }
133:
134: public void writeShorts(short[] s, int off, int len)
135: throws IOException {
136: if ((off < 0) || (len < 0) || (off + len > s.length)) {
137: throw new IndexOutOfBoundsException();
138: }
139:
140: for (int i = 0; i < len; i++) {
141: writeShort(s[off + i]);
142: }
143: }
144:
145: public void writeChars(char[] c, int off, int len)
146: throws IOException {
147: if ((off < 0) || (len < 0) || (off + len > c.length)) {
148: throw new IndexOutOfBoundsException();
149: }
150:
151: for (int i = 0; i < len; i++) {
152: writeShort(c[off + i]);
153: }
154: }
155:
156: public void writeInts(int[] i, int off, int len) throws IOException {
157: if ((off < 0) || (len < 0) || (off + len > i.length)) {
158: throw new IndexOutOfBoundsException();
159: }
160:
161: for (int n = 0; n < len; n++) {
162: writeInt(i[off + n]);
163: }
164: }
165:
166: public void writeLongs(long[] l, int off, int len)
167: throws IOException {
168: if ((off < 0) || (len < 0) || (off + len > l.length)) {
169: throw new IndexOutOfBoundsException();
170: }
171:
172: for (int i = 0; i < len; i++) {
173: writeLong(l[off + i]);
174: }
175: }
176:
177: public void writeFloats(float[] f, int off, int len)
178: throws IOException {
179: if ((off < 0) || (len < 0) || (off + len > f.length)) {
180: throw new IndexOutOfBoundsException();
181: }
182:
183: for (int i = 0; i < len; i++) {
184: writeFloat(f[off + i]);
185: }
186: }
187:
188: public void writeDoubles(double[] d, int off, int len)
189: throws IOException {
190: if ((off < 0) || (len < 0) || (off + len > d.length)) {
191: throw new IndexOutOfBoundsException();
192: }
193:
194: for (int i = 0; i < len; i++) {
195: writeDouble(d[off + i]);
196: }
197: }
198:
199: public void writeBit(int bit) throws IOException,
200: NotImplementedException {
201: // TODO: implement
202: throw new NotImplementedException();
203: }
204:
205: public void writeBits(long bits, int numBits) throws IOException,
206: NotImplementedException {
207: // TODO: implement
208: throw new NotImplementedException();
209: }
210:
211: protected final void flushBits() throws IOException,
212: NotImplementedException {
213: if (bitOffset == 0) {
214: return;
215: }
216:
217: // TODO: implement
218: throw new NotImplementedException();
219: }
220: }
|