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 Base32 decoding in a streaming fashion (unlimited size). When encoding the default lineLength is 76 characters and the default lineEnding is CRLF, 027 * but these can be overridden by using the appropriate constructor. 028 * <p> 029 * The default behavior of the Base32InputStream is to DECODE, whereas the default behavior of the Base32OutputStream is to ENCODE, but this behavior can be 030 * overridden by using a different constructor. 031 * </p> 032 * <p> 033 * Since this class operates directly on byte streams, and not character streams, it is hard-coded to only encode/decode character encodings which are 034 * compatible with the lower 127 ASCII chart (ISO-8859-1, Windows-1252, UTF-8, etc). 035 * </p> 036 * <p> 037 * You can set the decoding behavior when the input bytes contain leftover trailing bits that cannot be created by a valid encoding. These can be bits that are 038 * unused from the final character or entire characters. The default mode is lenient decoding. 039 * </p> 040 * <ul> 041 * <li>Lenient: Any trailing bits are composed into 8-bit bytes where possible. The remainder are discarded. 042 * <li>Strict: The decoding will raise an {@link IllegalArgumentException} if trailing bits are not part of a valid encoding. Any unused bits from the final 043 * character must be zero. Impossible counts of entire final characters are not allowed. 044 * </ul> 045 * <p> 046 * When strict decoding is enabled it is expected that the decoded bytes will be re-encoded to a byte array that matches the original, i.e. no changes occur on 047 * the final character. This requires that the input bytes use the same padding and alphabet as the encoder. 048 * </p> 049 * 050 * @see Base32 051 * @see <a href="https://www.ietf.org/rfc/rfc4648.txt">RFC 4648</a> 052 * @since 1.5 053 */ 054public class Base32InputStream extends BaseNCodecInputStream<Base32, Base32InputStream, Base32InputStream.Builder> { 055 056 /** 057 * Builds instances of Base32InputStream. 058 * 059 * @since 1.20.0 060 */ 061 public static class Builder extends AbstracBuilder<Base32InputStream, Base32, Builder> { 062 063 /** 064 * Constructs a new instance. 065 */ 066 public Builder() { 067 // empty 068 } 069 070 @Override 071 public Base32InputStream get() { 072 return new Base32InputStream(this); 073 } 074 075 @Override 076 protected Base32 newBaseNCodec() { 077 return new Base32(); 078 } 079 } 080 081 /** 082 * Constructs a new Builder. 083 * 084 * @return a new Builder. 085 * @since 1.20.0 086 */ 087 public static Builder builder() { 088 return new Builder(); 089 } 090 091 private Base32InputStream(final Builder builder) { 092 super(builder); 093 } 094 095 /** 096 * Constructs a Base32InputStream such that all data read is Base32-decoded from the original provided InputStream. 097 * 098 * @param inputStream InputStream to wrap. 099 */ 100 public Base32InputStream(final InputStream inputStream) { 101 super(builder().setInputStream(inputStream)); 102 } 103 104 /** 105 * Constructs a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original provided InputStream. 106 * 107 * @param inputStream InputStream to wrap. 108 * @param encode true if we should encode all data read from us, false if we should decode. 109 * @deprecated Use {@link #builder()} and {@link Builder}. 110 */ 111 @Deprecated 112 public Base32InputStream(final InputStream inputStream, final boolean encode) { 113 super(builder().setInputStream(inputStream).setEncode(encode)); 114 } 115 116 /** 117 * Constructs a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original provided InputStream. 118 * 119 * @param inputStream InputStream to wrap. 120 * @param encode true if we should encode all data read from us, false if we should decode. 121 * @param lineLength If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to the nearest multiple of 4). If 122 * lineLength <= 0, the encoded data is not divided into lines. If doEncode is false, lineLength is ignored. 123 * @param lineSeparator If doEncode is true, each line of encoded data will be terminated with this byte sequence (for example \r\n). If lineLength <= 0, 124 * the lineSeparator is not used. If doEncode is false lineSeparator is ignored. 125 * @deprecated Use {@link #builder()} and {@link Builder}. 126 */ 127 @Deprecated 128 public Base32InputStream(final InputStream inputStream, final boolean encode, final int lineLength, final byte[] lineSeparator) { 129 super(builder().setInputStream(inputStream).setEncode(encode) 130 .setBaseNCodec(Base32.builder().setLineLength(lineLength).setLineSeparator(lineSeparator).get())); 131 } 132 133 /** 134 * Constructs a Base32InputStream such that all data read is either Base32-encoded or Base32-decoded from the original provided InputStream. 135 * 136 * @param inputStream InputStream to wrap. 137 * @param encode true if we should encode all data read from us, false if we should decode. 138 * @param lineLength If doEncode is true, each line of encoded data will contain lineLength characters (rounded down to the nearest multiple of 4). If 139 * lineLength <= 0, the encoded data is not divided into lines. If doEncode is false, lineLength is ignored. 140 * @param lineSeparator If doEncode is true, each line of encoded data will be terminated with this byte sequence (for example \r\n). If lineLength <= 141 * 0, the lineSeparator is not used. If doEncode is false lineSeparator is ignored. 142 * @param decodingPolicy The decoding policy. 143 * @since 1.15 144 * @deprecated Use {@link #builder()} and {@link Builder}. 145 */ 146 @Deprecated 147 public Base32InputStream(final InputStream inputStream, final boolean encode, final int lineLength, final byte[] lineSeparator, 148 final CodecPolicy decodingPolicy) { 149 super(builder().setInputStream(inputStream).setEncode(encode) 150 .setBaseNCodec(Base32.builder().setLineLength(lineLength).setLineSeparator(lineSeparator).setDecodingPolicy(decodingPolicy).get())); 151 } 152}