1 package nom.tam.util;
2
3 /*
4 * #%L
5 * nom.tam FITS library
6 * %%
7 * Copyright (C) 1996 - 2024 nom-tam-fits
8 * %%
9 * This is free and unencumbered software released into the public domain.
10 *
11 * Anyone is free to copy, modify, publish, use, compile, sell, or
12 * distribute this software, either in source code form or as a compiled
13 * binary, for any purpose, commercial or non-commercial, and by any
14 * means.
15 *
16 * In jurisdictions that recognize copyright laws, the author or authors
17 * of this software dedicate any and all copyright interest in the
18 * software to the public domain. We make this dedication for the benefit
19 * of the public at large and to the detriment of our heirs and
20 * successors. We intend this dedication to be an overt act of
21 * relinquishment in perpetuity of all present and future rights to this
22 * software under copyright law.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
27 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
28 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
29 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
30 * OTHER DEALINGS IN THE SOFTWARE.
31 * #L%
32 */
33
34 import java.io.IOException;
35
36 /**
37 * @deprecated Use {@link FitsEncoder} instead which provides a similar function but in a more consistent way and with a
38 * less misleading name. This is a rusty rail implementation for of an older abandoned class only,
39 * unsafe for general use. For writing non-FITS encoding you may also use {@link OutputEncoder} as a
40 * base for implementing efficient custom encoding of binary outputs in general.
41 *
42 * @see FitsEncoder
43 */
44 @Deprecated
45 public abstract class BufferEncoder extends FitsEncoder {
46
47 private BufferPointer p;
48
49 /**
50 * Instantiates a new encoder for FITS data types.
51 *
52 * @param p Unused, but the position and length fields are set/reset as to pretend that the buffer is perpetually
53 * half filled with data, and with position at 0. However, at no point will there be any data actually
54 * in the buffer of this object. You should by all means avoid directly writing data from this buffer
55 * to the output stream, other than the hopefully untriggered write of an existing
56 * <code>needBuffer(int)</code> implementation (and it's safest if you don't override or ever call
57 * <code>needBuffer(int)</code> from your code!).
58 */
59 @Deprecated
60 public BufferEncoder(BufferPointer p) {
61 super();
62
63 this.p = p;
64
65 pretendHalfPopulated();
66
67 setOutput(new OutputWriter() {
68
69 private byte[] b1 = new byte[1];
70
71 @Override
72 public void write(int b) throws IOException {
73 b1[0] = (byte) b;
74 BufferEncoder.this.write(b1, 0, 1);
75 }
76
77 @Override
78 public void write(byte[] b, int from, int length) throws IOException {
79 BufferEncoder.this.write(b, from, length);
80 }
81
82 });
83 }
84
85 /**
86 * We'll always pretend the buffer to be half populated at pos=0, in order to avoid triggering a read from the input
87 * into the unused buffer of BufferPointer, or a write to the output from that buffer... If the pointer has no
88 * buffer, length will be 0 also.
89 */
90 private void pretendHalfPopulated() {
91 p.pos = 0;
92 p.length = p.buffer == null ? 0 : p.buffer.length >>> 1;
93 }
94
95 /**
96 * @deprecated No longer used internally, kept only for back-compatibility since it used to be a needed
97 * abstract method. It's safest if you never override or call this method from your
98 * code!
99 *
100 * @param need the number of consecutive bytes we need available in the conversion buffer
101 *
102 * @throws IOException if the buffer could not be flushed to the output to free up space in the buffer.
103 */
104 @Deprecated
105 protected void needBuffer(int need) throws IOException {
106 }
107
108 @Deprecated
109 @Override
110 void need(int bytes) throws IOException {
111 pretendHalfPopulated();
112 super.need(bytes);
113 }
114
115 @Deprecated
116 @Override
117 protected void write(byte[] b, int from, int len) throws IOException {
118 throw new UnsupportedOperationException(
119 "You need to override this with an implementation that writes to the desired output.");
120 }
121
122 /**
123 * Writes a single byte to the output, but not before flushing the contents of the conversion buffer. The supplied
124 * {@link BufferPointer} is not used at all, and is immediately invalidated (which is consistent with having flushed
125 * all pending output). It's not all that efficient, but then again one should be using the new {@link FitsEncoder}
126 * instead. This is really just a rusty rail solution. Also, since this methods does not throw an exception, and
127 * {@link #needBuffer(int)} (which did throw an exception) is no longer in use, the duct-tape solution is to convert
128 * any IOException encountered here into a runtime exception...
129 *
130 * @param b the byte to write
131 *
132 * @throws IllegalStateException if there was an IO error flushing the conversion buffer or writing the new byte
133 * after it.
134 */
135 @Deprecated
136 protected void writeUncheckedByte(byte b) throws IllegalStateException {
137 try {
138 flush();
139 write(b);
140 } catch (IOException e) {
141 throw new IllegalStateException(e);
142 }
143 }
144 }