1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 package nom.tam.util;
32
33 import java.io.DataOutput;
34 import java.io.DataOutputStream;
35 import java.io.IOException;
36 import java.io.OutputStream;
37
38
39
40
41
42
43
44
45
46
47
48
49 @SuppressWarnings("deprecation")
50 public class FitsOutputStream extends ArrayOutputStream implements FitsOutput {
51
52
53 private final DataOutput data;
54
55
56 private int unencodedCount;
57
58
59 private Object lock = new Object();
60
61
62
63
64
65
66 public FitsOutputStream(OutputStream o) {
67 this(o, FitsIO.DEFAULT_BUFFER_SIZE);
68 }
69
70
71
72
73
74
75
76 public FitsOutputStream(OutputStream o, int bufLength) {
77 super(o, bufLength);
78 setEncoder(new FitsEncoder(this));
79 if (o instanceof DataOutput) {
80 data = (DataOutput) o;
81 } else {
82 data = new DataOutputStream(o);
83 }
84 }
85
86 @Override
87 protected FitsEncoder getEncoder() {
88 return (FitsEncoder) super.getEncoder();
89 }
90
91 @Override
92 public synchronized void write(int b) throws IOException {
93 synchronized (lock) {
94 super.write(b);
95 unencodedCount++;
96 }
97 }
98
99 @Override
100 public synchronized void write(byte[] b, int start, int length) throws IOException {
101 synchronized (lock) {
102 super.write(b, start, length);
103 unencodedCount += length;
104 }
105 }
106
107 @Override
108 public void write(boolean[] b, int start, int length) throws IOException {
109 getEncoder().write(b, start, length);
110 }
111
112 @Override
113 public void write(Boolean[] buf, int offset, int size) throws IOException {
114 getEncoder().write(buf, offset, size);
115 }
116
117 @Override
118 public void write(char[] c, int start, int length) throws IOException {
119 getEncoder().write(c, start, length);
120 }
121
122 @Override
123 public void write(short[] s, int start, int length) throws IOException {
124 getEncoder().write(s, start, length);
125 }
126
127 @Override
128 public void write(int[] i, int start, int length) throws IOException {
129 getEncoder().write(i, start, length);
130 }
131
132 @Override
133 public void write(long[] l, int start, int length) throws IOException {
134 getEncoder().write(l, start, length);
135 }
136
137 @Override
138 public void write(float[] f, int start, int length) throws IOException {
139 getEncoder().write(f, start, length);
140 }
141
142 @Override
143 public void write(double[] d, int start, int length) throws IOException {
144 getEncoder().write(d, start, length);
145 }
146
147 @Override
148 public void writeBytes(String s) throws IOException {
149 getEncoder().writeBytes(s);
150 }
151
152 @Override
153 public void writeChars(String s) throws IOException {
154 getEncoder().writeChars(s);
155 }
156
157 @Override
158 public void writeUTF(String s) throws IOException {
159 data.writeUTF(s);
160 }
161
162 @Override
163 public void write(String[] s, int start, int len) throws IOException {
164 getEncoder().write(s, start, len);
165 }
166
167 @Override
168 public void writeByte(int b) throws IOException {
169 getEncoder().writeByte(b);
170 }
171
172 @Override
173 public void writeBoolean(boolean b) throws IOException {
174 getEncoder().writeBoolean(b);
175 }
176
177 @Override
178 public void writeChar(int c) throws IOException {
179 getEncoder().writeChar(c);
180 }
181
182 @Override
183 public void writeShort(int s) throws IOException {
184 getEncoder().writeShort(s);
185 }
186
187 @Override
188 public void writeInt(int i) throws IOException {
189 getEncoder().writeInt(i);
190 }
191
192 @Override
193 public void writeLong(long l) throws IOException {
194 getEncoder().writeLong(l);
195 }
196
197 @Override
198 public void writeFloat(float f) throws IOException {
199 getEncoder().writeFloat(f);
200 }
201
202 @Override
203 public void writeDouble(double d) throws IOException {
204 getEncoder().writeDouble(d);
205 }
206
207
208
209
210
211
212
213
214
215
216 @Deprecated
217 public final void writePrimitiveArray(Object o) throws IOException {
218 writeArray(o);
219 }
220
221 @Override
222 public boolean isAtStart() {
223 return (unencodedCount + getEncoder().getCount()) == 0;
224 }
225
226 }