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   * Rotate every byte by an offset (either positiv or negativ).
12   *
13   * <p>SPDX-License-Identifier: MIT
14   * @author Kai Kretschmann
15   */
16  public class RotateLayer extends AbstractBasicLayer
17  {
18  
19      /**
20       * static name of the layer.
21       */
22      public static final String LAYERNAME = "Rotate Layer";
23  
24      /**
25       * private range to rate.
26       */
27      private int offset;
28  
29      /**
30       * Constructor of XorLayer.
31       */
32      public RotateLayer()
33      {
34          super(RotateLayer.class, LAYERNAME);
35      }
36  
37      /**
38       * init function.
39       *
40       * @param data to initialize the offset value.
41       */
42      @Override
43      public final void init(final String data)
44      {
45          this.offset = Integer.parseInt(data);
46      }
47  
48      /**
49       * encode Stream function.
50       *
51       * @param inputStream incoming data
52       * @param outputStream outgoing data
53       * @throws JastacryException thrown on error
54       */
55      @Override
56      public final void encStream(final InputStream inputStream, final OutputStream outputStream) throws JastacryException
57      {
58          try
59          {
60              int iChar;
61              while ((iChar = inputStream.read()) != -1)
62              { // NOPMD by kai on 21.11.17 17:19
63                  iChar += this.offset;
64                  iChar = rangeCheck(iChar);
65                  outputStream.write(iChar);
66              }
67              logger.info("close pipe");
68              outputStream.close();
69          }
70          catch (IOException e)
71          {
72              throw (JastacryExceptionl#JastacryException">JastacryException) new JastacryException("encStream failed").initCause(e);
73          }
74      }
75  
76      /**
77       * decode Stream function.
78       *
79       * @param inputStream incoming data
80       * @param outputStream outgoging data
81       * @throws JastacryException thrown on error
82       */
83      @Override
84      public final void decStream(final InputStream inputStream, final OutputStream outputStream) throws JastacryException
85      {
86          try
87          {
88              int iChar;
89              while ((iChar = inputStream.read()) != -1)
90              { // NOPMD by kai on 21.11.17 17:19
91                  iChar -= this.offset;
92                  iChar = rangeCheck(iChar);
93                  outputStream.write(iChar);
94              }
95              logger.info("close pipe");
96              outputStream.close();
97          }
98          catch (IOException e)
99          {
100             throw (JastacryExceptionl#JastacryException">JastacryException) new JastacryException("decStream failed").initCause(e);
101         }
102     }
103 
104     @Override
105     public final void encodeAndDecode(final InputStream inputStream, final OutputStream outputStream) throws JastacryException
106     {
107         throw new UnsupportedOperationException();
108     }
109 
110     /**
111      * Override equals method from object class.
112      * @param o object to compare with
113      * @return true or false
114      */
115     @Override
116     public boolean equals(final Object o)
117     {
118         return o == this || o instanceof RotateLayer/org/jastacry/layer/RotateLayer.html#RotateLayer">RotateLayer && ((RotateLayer) o).offset == this.offset;
119     }
120 
121     /**
122      * Override equals method from object class.
123      * @return hash of properties
124      */
125     @Override
126     public int hashCode()
127     {
128         return Objects.hash(offset);
129     }
130 }