001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.codec.binary;
019
020import java.io.OutputStream;
021
022/**
023 * Provides <a href="https://datatracker.ietf.org/doc/html/rfc9285">RFC 9285 Base45</a> encoding in a streaming fashion (unlimited size).
024 * <p>
025 * <strong>Note:</strong> Call {@link #close()} or {@link #eof()} after the last byte has been written; otherwise the final
026 * partial block will be omitted and the resulting data will be incomplete.
027 * </p>
028 *
029 * @see Base45
030 * @see <a href="https://datatracker.ietf.org/doc/html/rfc9285">RFC 9285 – The Base45 Data Encoding</a>
031 * @since 1.23.0
032 */
033public class Base45OutputStream extends BaseNCodecOutputStream<Base45, Base45OutputStream, Base45OutputStream.Builder> {
034
035    /**
036     * Builds instances of Base45OutputStream.
037     */
038    public static class Builder extends BaseNCodecOutputStream.AbstractBuilder<Base45OutputStream, Base45, Builder> {
039
040        /**
041         * Constructs a new instance.
042         */
043        public Builder() {
044            setEncode(true);
045        }
046
047        /**
048         * Gets a new Base45OutputStream instance with the configured settings.
049         *
050         * @return A new Base45OutputStream.
051         */
052        @Override
053        public Base45OutputStream get() {
054            return new Base45OutputStream(this);
055        }
056
057        /**
058         * Creates a new Base45 codec instance.
059         *
060         * @return A new Base45 codec.
061         */
062        @Override
063        protected Base45 newBaseNCodec() {
064            return new Base45();
065        }
066    }
067
068    /**
069     * Constructs a new Builder.
070     *
071     * @return A new Builder.
072     */
073    public static Builder builder() {
074        return new Builder();
075    }
076
077    private Base45OutputStream(final Builder builder) {
078        super(builder);
079    }
080
081    /**
082     * Constructs a Base45OutputStream such that all data written is Base45-encoded to the original provided OutputStream.
083     *
084     * @param outputStream OutputStream to wrap.
085     */
086    public Base45OutputStream(final OutputStream outputStream) {
087        this(builder().setOutputStream(outputStream));
088    }
089}