View Javadoc
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    *      http://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  
18  package org.apache.commons.codec.binary;
19  
20  import java.io.OutputStream;
21  
22  import org.apache.commons.codec.CodecPolicy;
23  
24  /**
25   * Provides Hex encoding and decoding in a streaming fashion (unlimited size).
26   * <p>
27   * The default behavior of the HexOutputStream is to ENCODE, whereas the default behavior of the
28   * {@link Base16InputStream} is to DECODE. But this behavior can be overridden by using a different constructor.
29   * </p>
30   *
31   * @since 1.15
32   */
33  public class Base16OutputStream extends BaseNCodecOutputStream {
34  
35      /**
36       * Constructs a Base16OutputStream such that all data written is Hex-encoded to the original provided OutputStream.
37       *
38       * @param outputStream OutputStream to wrap.
39       */
40      public Base16OutputStream(final OutputStream outputStream) {
41          this(outputStream, true);
42      }
43  
44      /**
45       * Constructs a Base16OutputStream such that all data written is either Hex-encoded or Hex-decoded to the
46       * original provided OutputStream.
47       *
48       * @param outputStream OutputStream to wrap.
49       * @param doEncode true if we should encode all data written to us, false if we should decode.
50       */
51      public Base16OutputStream(final OutputStream outputStream, final boolean doEncode) {
52          this(outputStream, doEncode, false);
53      }
54  
55      /**
56       * Constructs a Base16OutputStream such that all data written is either Hex-encoded or Hex-decoded to the
57       * original provided OutputStream.
58       *
59       * @param outputStream OutputStream to wrap.
60       * @param doEncode true if we should encode all data written to us, false if we should decode.
61       * @param lowerCase if {@code true} then use a lower-case Base16 alphabet.
62       */
63      public Base16OutputStream(final OutputStream outputStream, final boolean doEncode, final boolean lowerCase) {
64          this(outputStream, doEncode, lowerCase, CodecPolicy.LENIENT);
65      }
66  
67      /**
68       * Constructs a Base16OutputStream such that all data written is either Hex-encoded or Hex-decoded to the
69       * original provided OutputStream.
70       *
71       * @param outputStream OutputStream to wrap.
72       * @param doEncode true if we should encode all data written to us, false if we should decode.
73       * @param lowerCase if {@code true} then use a lower-case Base16 alphabet.
74       * @param decodingPolicy Decoding policy.
75       */
76      public Base16OutputStream(final OutputStream outputStream, final boolean doEncode, final boolean lowerCase, final CodecPolicy decodingPolicy) {
77          super(outputStream, new Base16(lowerCase, decodingPolicy), doEncode);
78      }
79  }