View Javadoc
1   package org.jastacry.layer;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.IOException;
5   import java.io.InputStream;
6   import java.io.OutputStream;
7   import java.util.Base64;
8   import java.util.Objects;
9   
10  import org.apache.commons.io.IOUtils;
11  import org.jastacry.JastacryException;
12  
13  /**
14   * Helper class for encode decode.
15   *
16   * <p>SPDX-License-Identifier: MIT
17   *
18   * @author Kai Kretschmann
19   */
20  public class AsciiTransportLayer extends AbstractBasicLayer
21  {
22      /**
23       * static name of the layer.
24       */
25      public static final String LAYERNAME = "ASCII Layer";
26  
27      /**
28       * Constructor of EncodeDecodeLayer.
29       */
30      public AsciiTransportLayer()
31      {
32          super(AsciiTransportLayer.class, LAYERNAME);
33      }
34  
35      /**
36       * init function.
37       *
38       * @param data to initialise nothing.
39       */
40      @Override
41      public final void init(final String data)
42      {
43          // Empty at the moment
44      }
45  
46      /**
47       * encode Stream function.
48       *
49       * @param inputStream incoming data
50       * @param outputStream outgoing data
51       * @throws JastacryException thrown on error
52       */
53      @Override
54      public final void encStream(final InputStream inputStream, final OutputStream outputStream) throws JastacryException
55      {
56          try
57          {
58              final Base64.Encoder encoder = Base64.getEncoder();
59              final byte[] bytes = IOUtils.toByteArray(inputStream);
60              final byte[] bytesEncoded = encoder.encode(bytes);
61              final ByteArrayInputStream inputByteStream = new ByteArrayInputStream(bytesEncoded);
62              final int iCount = IOUtils.copy(inputByteStream, outputStream);
63              logger.debug("encStream copied {} bytes", iCount);
64          }
65          catch (IOException e)
66          {
67              throw (JastacryExceptionl#JastacryException">JastacryException) new JastacryException("encStream failed").initCause(e);
68          }
69      }
70  
71      /**
72       * decode Stream function.
73       *
74       * @param inputStream incoming data
75       * @param outputStream outgoing data
76       * @throws JastacryException thrown on error
77       */
78      @Override
79      public final void decStream(final InputStream inputStream, final OutputStream outputStream) throws JastacryException
80      {
81          try
82          {
83              final Base64.Decoder decoder = Base64.getDecoder();
84              final InputStream isDecoded = decoder.wrap(inputStream);
85              final int iCount = IOUtils.copy(isDecoded, outputStream);
86              logger.debug("decStream copied {} bytes", iCount);
87          }
88          catch (IOException e)
89          {
90              throw (JastacryExceptionl#JastacryException">JastacryException) new JastacryException("encStream failed").initCause(e);
91          }
92      }
93  
94      @Override
95      public final void encodeAndDecode(final InputStream inputStream, final OutputStream outputStream) throws JastacryException
96      {
97          throw new UnsupportedOperationException();
98      }
99  
100     /**
101      * Override equals method from object class.
102      * @param o object to compare with
103      * @return true or false
104      */
105     @Override
106     public boolean equals(final Object o)
107     {
108         return o == this || o instanceof AsciiTransportLayer;
109     }
110 
111     /**
112      * Override equals method from object class.
113      * @return hash of properties
114      */
115     @Override
116     public int hashCode()
117     {
118         return Objects.hash();
119     }
120 }