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.OutputStream;
21  
22  import org.apache.commons.codec.CodecPolicy;
23  import org.apache.commons.codec.binary.BaseNCodecOutputStream.AbstractBuilder; // NOPMD: Required by ECJ (Eclipse)
24  
25  /**
26   * Provides Base16 encoding in a streaming fashion (unlimited size).
27   * <p>
28   * The default behavior of the Base16OutputStream is to ENCODE, whereas the default behavior of the {@link Base16InputStream} is to DECODE. 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 Base16OutputStream extends BaseNCodecOutputStream<Base16, Base16OutputStream, Base16OutputStream.Builder> {
36  
37      /**
38       * Builds instances of Base16InputStream.
39       */
40      public static class Builder extends AbstractBuilder<Base16OutputStream, Base16, Builder> {
41  
42          /**
43           * Constructs a new instance.
44           */
45          public Builder() {
46              setEncode(true);
47          }
48  
49          @Override
50          public Base16OutputStream get() {
51              return new Base16OutputStream(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 Base16OutputStream(final Builder builder) {
70          super(builder);
71      }
72  
73      /**
74       * Constructs a Base16OutputStream such that all data written is Base16-encoded to the original provided OutputStream.
75       *
76       * @param outputStream OutputStream to wrap.
77       */
78      public Base16OutputStream(final OutputStream outputStream) {
79          super(builder().setOutputStream(outputStream));
80      }
81  
82      /**
83       * Constructs a Base16OutputStream such that all data written is either Base16-encoded or Base16-decoded to the original provided OutputStream.
84       *
85       * @param outputStream OutputStream to wrap.
86       * @param encode     true if we should encode all data written to us, false if we should decode.
87       * @deprecated Use {@link #builder()} and {@link Builder}.
88       */
89      @Deprecated
90      public Base16OutputStream(final OutputStream outputStream, final boolean encode) {
91          super(builder().setOutputStream(outputStream).setEncode(encode));
92      }
93  
94      /**
95       * Constructs a Base16OutputStream such that all data written is either Base16-encoded or Base16-decoded to the original provided OutputStream.
96       *
97       * @param outputStream OutputStream to wrap.
98       * @param encode     true if we should encode all data written to us, false if we should decode.
99       * @param lowerCase    if {@code true} then use a lower-case Base16 alphabet.
100      * @deprecated Use {@link #builder()} and {@link Builder}.
101      */
102     @Deprecated
103     public Base16OutputStream(final OutputStream outputStream, final boolean encode, final boolean lowerCase) {
104         super(builder().setOutputStream(outputStream).setEncode(encode).setBaseNCodec(new Base16(lowerCase)));
105     }
106 
107     /**
108      * Constructs a Base16OutputStream such that all data written is either Base16-encoded or Base16-decoded to the original provided OutputStream.
109      *
110      * @param outputStream   OutputStream to wrap.
111      * @param encode       true if we should encode all data written to us, false if we should decode.
112      * @param lowerCase      if {@code true} then use a lower-case Base16 alphabet.
113      * @param decodingPolicy Decoding policy.
114      * @deprecated Use {@link #builder()} and {@link Builder}.
115      */
116     @Deprecated
117     public Base16OutputStream(final OutputStream outputStream, final boolean encode, final boolean lowerCase, final CodecPolicy decodingPolicy) {
118         super(builder().setOutputStream(outputStream).setEncode(encode).setBaseNCodec(new Base16(lowerCase, decodingPolicy)));
119     }
120 }