Skip to content

Commit ae171e6

Browse files
committed
support any output stream
1 parent 8f15140 commit ae171e6

1 file changed

Lines changed: 62 additions & 9 deletions

File tree

src/main/java/mil/nga/sf/util/ByteWriter.java

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import java.io.ByteArrayOutputStream;
44
import java.io.IOException;
5+
import java.io.OutputStream;
56
import java.nio.ByteBuffer;
67
import java.nio.ByteOrder;
8+
import java.util.logging.Level;
9+
import java.util.logging.Logger;
710

811
/**
912
* Write a byte array
@@ -12,10 +15,16 @@
1215
*/
1316
public class ByteWriter {
1417

18+
/**
19+
* Logger
20+
*/
21+
private static final Logger logger = Logger
22+
.getLogger(ByteWriter.class.getName());
23+
1524
/**
1625
* Output stream to write bytes to
1726
*/
18-
private final ByteArrayOutputStream os = new ByteArrayOutputStream();
27+
private final OutputStream outputStream;
1928

2029
/**
2130
* Byte order
@@ -26,15 +35,57 @@ public class ByteWriter {
2635
* Constructor
2736
*/
2837
public ByteWriter() {
38+
outputStream = new ByteArrayOutputStream();
39+
}
40+
41+
/**
42+
* Constructor
43+
*
44+
* @param outputStream
45+
* output stream
46+
*
47+
* @since 2.0.3
48+
*/
49+
public ByteWriter(OutputStream outputStream) {
50+
this.outputStream = outputStream;
51+
}
52+
53+
/**
54+
* Get the output stream
55+
*
56+
* @return output stream
57+
*
58+
* @since 2.0.3
59+
*/
60+
public OutputStream getOutputStream() {
61+
return outputStream;
62+
}
63+
64+
/**
65+
* Get the output stream
66+
*
67+
* @return output stream
68+
*
69+
* @since 2.0.3
70+
*/
71+
public ByteArrayOutputStream getByteArrayOutputStream() {
72+
if (!(outputStream instanceof ByteArrayOutputStream)) {
73+
throw new SFException(
74+
"Output Stream is not a ByteArrayOutputStream: "
75+
+ outputStream.getClass().getName());
76+
}
77+
return (ByteArrayOutputStream) outputStream;
2978
}
3079

3180
/**
3281
* Close the byte writer
3382
*/
3483
public void close() {
3584
try {
36-
os.close();
85+
outputStream.close();
3786
} catch (IOException e) {
87+
logger.log(Level.WARNING,
88+
"Failed to close byte writer output stream", e);
3889
}
3990
}
4091

@@ -63,7 +114,7 @@ public void setByteOrder(ByteOrder byteOrder) {
63114
* @return written bytes
64115
*/
65116
public byte[] getBytes() {
66-
return os.toByteArray();
117+
return getByteArrayOutputStream().toByteArray();
67118
}
68119

69120
/**
@@ -72,7 +123,7 @@ public byte[] getBytes() {
72123
* @return bytes written
73124
*/
74125
public int size() {
75-
return os.size();
126+
return getByteArrayOutputStream().size();
76127
}
77128

78129
/**
@@ -85,17 +136,19 @@ public int size() {
85136
*/
86137
public void writeString(String value) throws IOException {
87138
byte[] valueBytes = value.getBytes();
88-
os.write(valueBytes);
139+
outputStream.write(valueBytes);
89140
}
90141

91142
/**
92143
* Write a byte
93144
*
94145
* @param value
95146
* byte
147+
* @throws IOException
148+
* upon error
96149
*/
97-
public void writeByte(byte value) {
98-
os.write(value);
150+
public void writeByte(byte value) throws IOException {
151+
outputStream.write(value);
99152
}
100153

101154
/**
@@ -112,7 +165,7 @@ public void writeInt(int value) throws IOException {
112165
.putInt(value);
113166
byteBuffer.flip();
114167
byteBuffer.get(valueBytes);
115-
os.write(valueBytes);
168+
outputStream.write(valueBytes);
116169
}
117170

118171
/**
@@ -129,7 +182,7 @@ public void writeDouble(double value) throws IOException {
129182
.putDouble(value);
130183
byteBuffer.flip();
131184
byteBuffer.get(valueBytes);
132-
os.write(valueBytes);
185+
outputStream.write(valueBytes);
133186
}
134187

135188
}

0 commit comments

Comments
 (0)