View Javadoc
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       * @param p Unused, but the position and length fields are set/reset as to pretend that the buffer is perpetually
51       *              half filled with data, and with position at 0. However, at no point will there be any data actually
52       *              in the buffer of this object. You should by all means avoid directly writing data from this buffer
53       *              to the output stream, other than the hopefully untriggered write of an existing
54       *              <code>needBuffer(int)</code> implementation (and it's safest if you don't override or ever call
55       *              <code>needBuffer(int)</code> from your code!).
56       */
57      public BufferEncoder(BufferPointer p) {
58          super();
59  
60          this.p = p;
61  
62          pretendHalfPopulated();
63  
64          setOutput(new OutputWriter() {
65  
66              private byte[] b1 = new byte[1];
67  
68              @Override
69              public void write(int b) throws IOException {
70                  b1[0] = (byte) b;
71                  BufferEncoder.this.write(b1, 0, 1);
72              }
73  
74              @Override
75              public void write(byte[] b, int from, int length) throws IOException {
76                  BufferEncoder.this.write(b, from, length);
77              }
78  
79          });
80      }
81  
82      /**
83       * We'll always pretend the buffer to be half populated at pos=0, in order to avoid triggering a read from the input
84       * into the unused buffer of BufferPointer, or a write to the output from that buffer... If the pointer has no
85       * buffer, length will be 0 also.
86       */
87      private void pretendHalfPopulated() {
88          p.pos = 0;
89          p.length = p.buffer == null ? 0 : p.buffer.length >>> 1;
90      }
91  
92      /**
93       * @deprecated             No longer used internally, kept only for back-compatibility since it used to be a needed
94       *                             abstract method. It's safest if you never override or call this method from your
95       *                             code!
96       *
97       * @param      need        the number of consecutive bytes we need available in the conversion buffer
98       *
99       * @throws     IOException if the buffer could not be flushed to the output to free up space in the buffer.
100      */
101     @Deprecated
102     protected void needBuffer(int need) throws IOException {
103     }
104 
105     @Override
106     void need(int bytes) throws IOException {
107         pretendHalfPopulated();
108         super.need(bytes);
109     }
110 
111     @Override
112     protected void write(byte[] b, int from, int len) throws IOException {
113         throw new UnsupportedOperationException(
114                 "You need to override this with an implementation that writes to the desired output.");
115     }
116 
117     /**
118      * Writes a single byte to the output, but not before flushing the contents of the conversion buffer. The supplied
119      * {@link BufferPointer} is not used at all, and is immediately invalidated (which is consistent with having flushed
120      * all pending output). It's not all that efficient, but then again one should be using the new {@link FitsEncoder}
121      * instead. This is really just a rusty rail solution. Also, since this methods does not throw an exception, and
122      * {@link #needBuffer(int)} (which did throw an exception) is no longer in use, the duct-tape solution is to convert
123      * any IOException encountered here into a runtime exception...
124      *
125      * @param  b                     the byte to write
126      *
127      * @throws IllegalStateException if there was an IO error flushing the conversion buffer or writing the new byte
128      *                                   after it.
129      */
130     protected void writeUncheckedByte(byte b) throws IllegalStateException {
131         try {
132             flush();
133             write(b);
134         } catch (IOException e) {
135             throw new IllegalStateException(e);
136         }
137     }
138 }