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    *      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  
18  package org.apache.commons.codec.binary;
19  
20  import java.io.InputStream;
21  
22  import org.apache.commons.codec.CodecPolicy;
23  import org.apache.commons.codec.binary.BaseNCodecInputStream.AbstracBuilder; // NOPMD: Required by ECJ (Eclipse)
24  
25  /**
26   * Provides Base16 decoding in a streaming fashion (unlimited size).
27   * <p>
28   * The default behavior of the Base16InputStream is to DECODE, whereas the default behavior of the {@link Base16OutputStream} is to ENCODE, but this behavior
29   * can be overridden by using a different constructor.
30   * </p>
31   *
32   * @see Base16
33   * @since 1.15
34   */
35  public class Base16InputStream extends BaseNCodecInputStream<Base16, Base16InputStream, Base16InputStream.Builder> {
36  
37      /**
38       * Builds instances of Base16InputStream.
39       */
40      public static class Builder extends AbstracBuilder<Base16InputStream, Base16, Builder> {
41  
42          /**
43           * Constructs a new instance.
44           */
45          public Builder() {
46              // empty
47          }
48  
49          @Override
50          public Base16InputStream get() {
51              return new Base16InputStream(this);
52          }
53  
54          @Override
55          protected Base16 newBaseNCodec() {
56              return new Base16();
57          }
58      }
59  
60      /**
61       * Constructs a new Builder.
62       *
63       * @return a new Builder.
64       */
65      public static Builder builder() {
66          return new Builder();
67      }
68  
69      private Base16InputStream(final Builder builder) {
70          super(builder);
71      }
72  
73      /**
74       * Constructs a Base16InputStream such that all data read is Base16-decoded from the original provided InputStream.
75       *
76       * @param inputStream InputStream to wrap.
77       */
78      public Base16InputStream(final InputStream inputStream) {
79          super(builder().setInputStream(inputStream));
80      }
81  
82      /**
83       * Constructs a Base16InputStream such that all data read is either Base16-encoded or Base16-decoded from the original provided InputStream.
84       *
85       * @param inputStream InputStream to wrap.
86       * @param encode    true if we should encode all data read from us, false if we should decode.
87       */
88      @Deprecated
89      public Base16InputStream(final InputStream inputStream, final boolean encode) {
90          super(builder().setInputStream(inputStream).setEncode(encode));
91      }
92  
93      /**
94       * Constructs a Base16InputStream such that all data read is either Base16-encoded or Base16-decoded from the original provided InputStream.
95       *
96       * @param inputStream InputStream to wrap.
97       * @param encode    true if we should encode all data read from us, false if we should decode.
98       * @param lowerCase   if {@code true} then use a lower-case Base16 alphabet.
99       */
100     @Deprecated
101     public Base16InputStream(final InputStream inputStream, final boolean encode, final boolean lowerCase) {
102         super(builder().setInputStream(inputStream).setEncode(encode).setBaseNCodec(new Base16(lowerCase)));
103     }
104 
105     /**
106      * Constructs a Base16InputStream such that all data read is either Base16-encoded or Base16-decoded from the original provided InputStream.
107      *
108      * @param inputStream    InputStream to wrap.
109      * @param encode       true if we should encode all data read from us, false if we should decode.
110      * @param lowerCase      if {@code true} then use a lower-case Base16 alphabet.
111      * @param decodingPolicy Decoding policy.
112      */
113     @Deprecated
114     public Base16InputStream(final InputStream inputStream, final boolean encode, final boolean lowerCase, final CodecPolicy decodingPolicy) {
115         super(builder().setInputStream(inputStream).setEncode(encode).setBaseNCodec(new Base16(lowerCase, decodingPolicy)));
116     }
117 }