AsciiTransportLayer.java

  1. package org.jastacry.layer;

  2. import java.io.ByteArrayInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. import java.util.Base64;
  7. import java.util.Objects;

  8. import org.apache.commons.io.IOUtils;
  9. import org.jastacry.JastacryException;

  10. /**
  11.  * Helper class for encode decode.
  12.  *
  13.  * <p>SPDX-License-Identifier: MIT
  14.  *
  15.  * @author Kai Kretschmann
  16.  */
  17. public class AsciiTransportLayer extends AbstractBasicLayer
  18. {
  19.     /**
  20.      * static name of the layer.
  21.      */
  22.     static final String LAYERNAME = "ASCII Layer";

  23.     /**
  24.      * Constructor of EncodeDecodeLayer.
  25.      */
  26.     public AsciiTransportLayer()
  27.     {
  28.         super(AsciiTransportLayer.class, LAYERNAME);
  29.     }

  30.     /**
  31.      * init function.
  32.      *
  33.      * @param data to initialise nothing.
  34.      */
  35.     @Override
  36.     public final void init(final String data)
  37.     {
  38.         // Empty at the moment
  39.     }

  40.     /**
  41.      * encode Stream function.
  42.      *
  43.      * @param inputStream incoming data
  44.      * @param outputStream outgoing data
  45.      * @throws JastacryException thrown on error
  46.      */
  47.     @Override
  48.     public final void encStream(final InputStream inputStream, final OutputStream outputStream) throws JastacryException
  49.     {
  50.         try
  51.         {
  52.             final Base64.Encoder encoder = Base64.getEncoder();
  53.             final byte[] bytes = IOUtils.toByteArray(inputStream);
  54.             final byte[] bytesEncoded = encoder.encode(bytes);
  55.             final ByteArrayInputStream inputByteStream = new ByteArrayInputStream(bytesEncoded);
  56.             final int iCount = IOUtils.copy(inputByteStream, outputStream);
  57.             progress(iCount);
  58.             logger.debug("encStream copied {} bytes", iCount);
  59.         }
  60.         catch (IOException e)
  61.         {
  62.             throw (JastacryException) new JastacryException("encStream failed").initCause(e);
  63.         }
  64.     }

  65.     /**
  66.      * decode Stream function.
  67.      *
  68.      * @param inputStream incoming data
  69.      * @param outputStream outgoing data
  70.      * @throws JastacryException thrown on error
  71.      */
  72.     @Override
  73.     public final void decStream(final InputStream inputStream, final OutputStream outputStream) throws JastacryException
  74.     {
  75.         try
  76.         {
  77.             final Base64.Decoder decoder = Base64.getDecoder();
  78.             final InputStream isDecoded = decoder.wrap(inputStream);
  79.             final int iCount = IOUtils.copy(isDecoded, outputStream);
  80.             progress(iCount);
  81.             logger.debug("decStream copied {} bytes", iCount);
  82.         }
  83.         catch (IOException e)
  84.         {
  85.             throw (JastacryException) new JastacryException("encStream failed").initCause(e);
  86.         }
  87.     }

  88.     @Override
  89.     public final void encodeAndDecode(final InputStream inputStream, final OutputStream outputStream) throws JastacryException
  90.     {
  91.         throw new UnsupportedOperationException();
  92.     }

  93.     /**
  94.      * Override equals method from object class.
  95.      * @param o object to compare with
  96.      * @return true or false
  97.      */
  98.     @Override
  99.     public boolean equals(final Object o)
  100.     {
  101.         return o == this || o instanceof AsciiTransportLayer;
  102.     }

  103.     /**
  104.      * Override equals method from object class.
  105.      * @return hash of properties
  106.      */
  107.     @Override
  108.     public int hashCode()
  109.     {
  110.         return Objects.hash();
  111.     }
  112. }