Base16InputStream.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      https://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.codec.binary;

  18. import java.io.InputStream;

  19. import org.apache.commons.codec.CodecPolicy;

  20. /**
  21.  * Provides Base16 decoding in a streaming fashion (unlimited size).
  22.  * <p>
  23.  * The default behavior of the Base16InputStream is to DECODE, whereas the default behavior of the
  24.  * {@link Base16OutputStream} is to ENCODE, but this behavior can be overridden by using a different constructor.
  25.  * </p>
  26.  *
  27.  * @since 1.15
  28.  */
  29. public class Base16InputStream extends BaseNCodecInputStream {

  30.     /**
  31.      * Constructs a Base16InputStream such that all data read is Base16-decoded from the original provided InputStream.
  32.      *
  33.      * @param inputStream InputStream to wrap.
  34.      */
  35.     public Base16InputStream(final InputStream inputStream) {
  36.         this(inputStream, false);
  37.     }

  38.     /**
  39.      * Constructs a Base16InputStream such that all data read is either Base16-encoded or Base16-decoded from the original
  40.      * provided InputStream.
  41.      *
  42.      * @param inputStream InputStream to wrap.
  43.      * @param doEncode true if we should encode all data read from us, false if we should decode.
  44.      */
  45.     public Base16InputStream(final InputStream inputStream, final boolean doEncode) {
  46.         this(inputStream, doEncode, false);
  47.     }

  48.     /**
  49.      * Constructs a Base16InputStream such that all data read is either Base16-encoded or Base16-decoded from the original
  50.      * provided InputStream.
  51.      *
  52.      * @param inputStream InputStream to wrap.
  53.      * @param doEncode true if we should encode all data read from us, false if we should decode.
  54.      * @param lowerCase if {@code true} then use a lower-case Base16 alphabet.
  55.      */
  56.     public Base16InputStream(final InputStream inputStream, final boolean doEncode,
  57.             final boolean lowerCase) {
  58.         this(inputStream, doEncode, lowerCase, CodecPolicy.LENIENT);
  59.     }

  60.     /**
  61.      * Constructs a Base16InputStream such that all data read is either Base16-encoded or Base16-decoded from the original
  62.      * provided InputStream.
  63.      *
  64.      * @param inputStream InputStream to wrap.
  65.      * @param doEncode true if we should encode all data read from us, false if we should decode.
  66.      * @param lowerCase if {@code true} then use a lower-case Base16 alphabet.
  67.      * @param decodingPolicy Decoding policy.
  68.      */
  69.     public Base16InputStream(final InputStream inputStream, final boolean doEncode,
  70.             final boolean lowerCase, final CodecPolicy decodingPolicy) {
  71.         super(inputStream, new Base16(lowerCase, decodingPolicy), doEncode);
  72.     }
  73. }