Base16OutputStream.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.OutputStream;

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

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

  30.     /**
  31.      * Constructs a Base16OutputStream such that all data written is Hex-encoded to the original provided OutputStream.
  32.      *
  33.      * @param outputStream OutputStream to wrap.
  34.      */
  35.     public Base16OutputStream(final OutputStream outputStream) {
  36.         this(outputStream, true);
  37.     }

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

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

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