View Javadoc
1   package org.jastacry.layer;
2   
3   import java.io.IOException;
4   import java.io.InputStream;
5   import java.io.OutputStream;
6   import java.util.Objects;
7   
8   import org.jastacry.JastacryException;
9   
10  /**
11   * Reverse every bits per byte. 0101 gives 1010 i.e.
12   *
13   * <p>SPDX-License-Identifier: MIT
14   *
15   * @author Kai Kretschmann
16   */
17  public class ReverseLayer extends AbstractBasicLayer
18  {
19      /**
20       * Number of bits to shift back to byte form.
21       */
22      private static final int SHIFTBITS = 24;
23  
24      /**
25       * Bit mask to remove anything above one byte.
26       */
27      private static final int MASKBITS = 0x00000FF;
28  
29      /**
30       * static name of the layer.
31       */
32      public static final String LAYERNAME = "Reverse Layer";
33  
34      /**
35       * Constructor of XorLayer.
36       */
37      public ReverseLayer()
38      {
39          super(ReverseLayer.class, LAYERNAME);
40      }
41  
42      /**
43       * init function.
44       *
45       * @param data to initialize the offset value.
46       */
47      @Override
48      public final void init(final String data)
49      {
50          // not used
51      }
52  
53      /**
54       * Local encode Stream function which does the real thing for Reverse Layer.
55       *
56       * @param inputStream input data stream
57       * @param outputStream output data stream
58       * @throws JastacryException in case of error
59       */
60      public final void encodeAndDecode(final InputStream inputStream, final OutputStream outputStream) throws JastacryException
61      {
62          try
63          {
64              int iChar;
65              while ((iChar = inputStream.read()) != -1)
66              {
67                  iChar = rangeCheck(iChar);
68                  iChar = Integer.reverse(iChar);
69                  iChar >>= SHIFTBITS;
70                  iChar &= MASKBITS;
71                  outputStream.write(iChar);
72              }
73              logger.info("close pipe");
74              outputStream.close();
75          }
76          catch (IOException e)
77          {
78              throw (JastacryExceptionl#JastacryException">JastacryException) new JastacryException("encodeAndDecode failed").initCause(e);
79          }
80      }
81  
82      /**
83       * Override equals method from object class.
84       * @param o object to compare with
85       * @return true or false
86       */
87      @Override
88      public boolean equals(final Object o)
89      {
90          return o == this || o instanceof ReverseLayer;
91      }
92  
93      /**
94       * Override equals method from object class.
95       * @return hash of properties
96       */
97      @Override
98      public int hashCode()
99      {
100         return Objects.hash();
101     }
102 }