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 *      http://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;
023
024/**
025 * Provides Hex encoding and decoding in a streaming fashion (unlimited size).
026 * <p>
027 * The default behavior of the HexOutputStream is to ENCODE, whereas the default behavior of the
028 * {@link Base16InputStream} is to DECODE. But this behavior can be overridden by using a different constructor.
029 * </p>
030 *
031 * @since 1.15
032 */
033public class Base16OutputStream extends BaseNCodecOutputStream {
034
035    /**
036     * Constructs a Base16OutputStream such that all data written is Hex-encoded to the original provided OutputStream.
037     *
038     * @param outputStream OutputStream to wrap.
039     */
040    public Base16OutputStream(final OutputStream outputStream) {
041        this(outputStream, true);
042    }
043
044    /**
045     * Constructs a Base16OutputStream such that all data written is either Hex-encoded or Hex-decoded to the
046     * original provided OutputStream.
047     *
048     * @param outputStream OutputStream to wrap.
049     * @param doEncode true if we should encode all data written to us, false if we should decode.
050     */
051    public Base16OutputStream(final OutputStream outputStream, final boolean doEncode) {
052        this(outputStream, doEncode, false);
053    }
054
055    /**
056     * Constructs a Base16OutputStream such that all data written is either Hex-encoded or Hex-decoded to the
057     * original provided OutputStream.
058     *
059     * @param outputStream OutputStream to wrap.
060     * @param doEncode true if we should encode all data written to us, false if we should decode.
061     * @param lowerCase if {@code true} then use a lower-case Base16 alphabet.
062     */
063    public Base16OutputStream(final OutputStream outputStream, final boolean doEncode, final boolean lowerCase) {
064        this(outputStream, doEncode, lowerCase, CodecPolicy.LENIENT);
065    }
066
067    /**
068     * Constructs a Base16OutputStream such that all data written is either Hex-encoded or Hex-decoded to the
069     * original provided OutputStream.
070     *
071     * @param outputStream OutputStream to wrap.
072     * @param doEncode true if we should encode all data written to us, false if we should decode.
073     * @param lowerCase if {@code true} then use a lower-case Base16 alphabet.
074     * @param decodingPolicy Decoding policy.
075     */
076    public Base16OutputStream(final OutputStream outputStream, final boolean doEncode, final boolean lowerCase, final CodecPolicy decodingPolicy) {
077        super(outputStream, new Base16(lowerCase, decodingPolicy), doEncode);
078    }
079}