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  /**
23   * Provides <a href="https://datatracker.ietf.org/doc/html/rfc9285">RFC 9285 Base45</a> encoding in a streaming fashion (unlimited size).
24   * <p>
25   * <strong>Note:</strong> Call {@link #close()} or {@link #eof()} after the last byte has been written; otherwise the final
26   * partial block will be omitted and the resulting data will be incomplete.
27   * </p>
28   *
29   * @see Base45
30   * @see <a href="https://datatracker.ietf.org/doc/html/rfc9285">RFC 9285 – The Base45 Data Encoding</a>
31   * @since 1.23.0
32   */
33  public class Base45OutputStream extends BaseNCodecOutputStream<Base45, Base45OutputStream, Base45OutputStream.Builder> {
34  
35      /**
36       * Builds instances of Base45OutputStream.
37       */
38      public static class Builder extends BaseNCodecOutputStream.AbstractBuilder<Base45OutputStream, Base45, Builder> {
39  
40          /**
41           * Constructs a new instance.
42           */
43          public Builder() {
44              setEncode(true);
45          }
46  
47          /**
48           * Gets a new Base45OutputStream instance with the configured settings.
49           *
50           * @return A new Base45OutputStream.
51           */
52          @Override
53          public Base45OutputStream get() {
54              return new Base45OutputStream(this);
55          }
56  
57          /**
58           * Creates a new Base45 codec instance.
59           *
60           * @return A new Base45 codec.
61           */
62          @Override
63          protected Base45 newBaseNCodec() {
64              return new Base45();
65          }
66      }
67  
68      /**
69       * Constructs a new Builder.
70       *
71       * @return A new Builder.
72       */
73      public static Builder builder() {
74          return new Builder();
75      }
76  
77      private Base45OutputStream(final Builder builder) {
78          super(builder);
79      }
80  
81      /**
82       * Constructs a Base45OutputStream such that all data written is Base45-encoded to the original provided OutputStream.
83       *
84       * @param outputStream OutputStream to wrap.
85       */
86      public Base45OutputStream(final OutputStream outputStream) {
87          this(builder().setOutputStream(outputStream));
88      }
89  }