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.InputStream;
021
022import org.apache.commons.codec.CodecPolicy;
023import org.apache.commons.codec.binary.BaseNCodecInputStream.AbstracBuilder; // NOPMD: Required by ECJ (Eclipse)
024
025/**
026 * Provides Base16 decoding in a streaming fashion (unlimited size).
027 * <p>
028 * The default behavior of the Base16InputStream is to DECODE, whereas the default behavior of the {@link Base16OutputStream} is to ENCODE, 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 Base16InputStream extends BaseNCodecInputStream<Base16, Base16InputStream, Base16InputStream.Builder> {
036
037    /**
038     * Builds instances of Base16InputStream.
039     */
040    public static class Builder extends AbstracBuilder<Base16InputStream, Base16, Builder> {
041
042        /**
043         * Constructs a new instance.
044         */
045        public Builder() {
046            // empty
047        }
048
049        @Override
050        public Base16InputStream get() {
051            return new Base16InputStream(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 Base16InputStream(final Builder builder) {
070        super(builder);
071    }
072
073    /**
074     * Constructs a Base16InputStream such that all data read is Base16-decoded from the original provided InputStream.
075     *
076     * @param inputStream InputStream to wrap.
077     */
078    public Base16InputStream(final InputStream inputStream) {
079        super(builder().setInputStream(inputStream));
080    }
081
082    /**
083     * Constructs a Base16InputStream such that all data read is either Base16-encoded or Base16-decoded from the original provided InputStream.
084     *
085     * @param inputStream InputStream to wrap.
086     * @param encode    true if we should encode all data read from us, false if we should decode.
087     */
088    @Deprecated
089    public Base16InputStream(final InputStream inputStream, final boolean encode) {
090        super(builder().setInputStream(inputStream).setEncode(encode));
091    }
092
093    /**
094     * Constructs a Base16InputStream such that all data read is either Base16-encoded or Base16-decoded from the original provided InputStream.
095     *
096     * @param inputStream InputStream to wrap.
097     * @param encode    true if we should encode all data read from us, false if we should decode.
098     * @param lowerCase   if {@code true} then use a lower-case Base16 alphabet.
099     */
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}