    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
    022import org.apache.commons.codec.CodecPolicy;
    023import org.apache.commons.codec.binary.BaseNCodecOutputStream.AbstractBuilder; // NOPMD: Required by ECJ (Eclipse)
    024
    025/**
    026 * Provides Base16 encoding in a streaming fashion (unlimited size).
    027 * <p>
    028 * The default behavior of the Base16OutputStream is to ENCODE, whereas the default behavior of the {@link Base16InputStream} is to DECODE. But this behavior
    029 * can be overridden by using a different constructor.
    030 * </p>
    031 *
    032 * @see Base16
    033 * @since 1.15
    034 */
    035public class Base16OutputStream extends BaseNCodecOutputStream<Base16, Base16OutputStream, Base16OutputStream.Builder> {
    036
    037    /**
    038     * Builds instances of Base16InputStream.
    039     */
    040    public static class Builder extends AbstractBuilder<Base16OutputStream, Base16, Builder> {
    041
    042        /**
    043         * Constructs a new instance.
    044         */
    045        public Builder() {
    046            setEncode(true);
    047        }
    048
    049        @Override
    050        public Base16OutputStream get() {
    051            return new Base16OutputStream(this);
    052        }
    053
    054        @Override
    055        protected Base16 newBaseNCodec() {
    056            return new Base16();
    057        }
    058    }
    059
    060    /**
    061     * Constructs a new Builder.
    062     *
    063     * @return a new Builder.
    064     */
    065    public static Builder builder() {
    066        return new Builder();
    067    }
    068
    069    private Base16OutputStream(final Builder builder) {
    070        super(builder);
    071    }
    072
    073    /**
    074     * Constructs a Base16OutputStream such that all data written is Base16-encoded to the original provided OutputStream.
    075     *
    076     * @param outputStream OutputStream to wrap.
    077     */
    078    public Base16OutputStream(final OutputStream outputStream) {
    079        super(builder().setOutputStream(outputStream));
    080    }
    081
    082    /**
    083     * Constructs a Base16OutputStream such that all data written is either Base16-encoded or Base16-decoded to the original provided OutputStream.
    084     *
    085     * @param outputStream OutputStream to wrap.
    086     * @param encode     true if we should encode all data written to us, false if we should decode.
    087     * @deprecated Use {@link #builder()} and {@link Builder}.
    088     */
    089    @Deprecated
    090    public Base16OutputStream(final OutputStream outputStream, final boolean encode) {
    091        super(builder().setOutputStream(outputStream).setEncode(encode));
    092    }
    093
    094    /**
    095     * Constructs a Base16OutputStream such that all data written is either Base16-encoded or Base16-decoded to the original provided OutputStream.
    096     *
    097     * @param outputStream OutputStream to wrap.
    098     * @param encode     true if we should encode all data written to us, false if we should decode.
    099     * @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}