pixelmed
OutputArrayOrStream.java
Go to the documentation of this file.
1 /* Copyright (c) 2015, David A. Clunie DBA Pixelmed Publishing. All rights reserved. */
2 
3 package com.pixelmed.codec.jpeg;
4 
5 import java.io.IOException;
6 import java.io.OutputStream;
7 
8 import java.nio.ByteOrder;
9 
22 public class OutputArrayOrStream {
23 
24  private static final String identString = "@(#) $Header: /userland/cvs/codec/com/pixelmed/codec/jpeg/OutputArrayOrStream.java,v 1.5 2016/01/16 13:30:09 dclunie Exp $";
25 
26  protected OutputStream out = null;
27  protected ByteOrder order = null;
28  protected byte[] byteValues = null;
29  protected short[] shortValues = null;
30  protected int byteOffset = 0;
31  protected int shortOffset = 0;
32 
34  // lazy allocation
35  }
36 
37  public OutputArrayOrStream(OutputStream out,ByteOrder order) {
38  this.out = out;
39  this.order = order;
40  }
41 
42  public OutputArrayOrStream(byte[] byteValues) {
43  this.byteValues = byteValues;
44  byteOffset = 0;
45  }
46 
47  public OutputArrayOrStream(short[] shortValues) {
48  this.shortValues = shortValues;
49  shortOffset = 0;
50  }
51 
52  public void setOutputStream(OutputStream out,ByteOrder order) throws IOException {
53  if (this.out != null || this.byteValues != null || this.shortValues != null) {
54  throw new IOException("Destination already allocated");
55  }
56  this.out = out;
57  this.order = order;
58  }
59 
65  public ByteOrder order() {
66  return out == null ? null : order;
67  }
68 
75  public void order(ByteOrder order) throws IOException {
76  if (out == null) {
77  throw new IOException("Cannot assign byte order if no OutputStream");
78  }
79  this.order = order;
80  }
81 
82  public void allocateByteArray(int length) throws IOException {
83  if (this.out != null || this.byteValues != null || this.shortValues != null) {
84  throw new IOException("Destination already allocated");
85  }
86  this.byteValues = new byte[length];
87  byteOffset = 0;
88  }
89 
90  public void allocateShortArray(int length) throws IOException {
91  if (this.out != null || this.byteValues != null || this.shortValues != null) {
92  throw new IOException("Destination already allocated");
93  }
94  this.shortValues = new short[length];
95  shortOffset = 0;
96  }
97 
98  public OutputStream getOutputStream() {
99  return out;
100  }
101 
102  public byte[] getByteArray() {
103  return byteValues;
104  }
105 
106  public short[] getShortArray() {
107  return shortValues;
108  }
109 
116  public void writeByte(int b) throws IOException {
117  if (out != null) {
118  out.write(b);
119  }
120  else if (byteValues != null) {
121  byteValues[byteOffset++] = (byte)b;
122  }
123  else if (shortValues != null) {
124  throw new IOException("Cannot write byte value to short array");
125  }
126  else {
127  throw new IOException("Byte array not allocated yet");
128  }
129  }
130 
137  public void writeShort(int s) throws IOException {
138  if (out != null) {
139  if (order == ByteOrder.LITTLE_ENDIAN) {
140  out.write(s);
141  out.write(s>>8);
142  }
143  else {
144  out.write(s>>8);
145  out.write(s);
146  }
147  }
148  else if (shortValues != null) {
149  shortValues[shortOffset++] = (short)s;
150  }
151  else if (byteValues != null) {
152  throw new IOException("Cannot write short value to byte array");
153  }
154  else {
155  throw new IOException("Short array not allocated yet");
156  }
157  }
158 
166  public void close() throws IOException {
167  if (out != null) {
168  out.close();
169  }
170  }
171 
172 }
void writeByte(int b)
Writes the specified byte to this output.
void setOutputStream(OutputStream out, ByteOrder order)
OutputArrayOrStream(OutputStream out, ByteOrder order)
void writeShort(int s)
Writes the specified short to this output.