+++ /dev/null
-/*\r
- * Licensed to the Apache Software Foundation (ASF) under one or more\r
- * contributor license agreements. See the NOTICE file distributed with\r
- * this work for additional information regarding copyright ownership.\r
- * The ASF licenses this file to You under the Apache License, Version 2.0\r
- * (the "License"); you may not use this file except in compliance with\r
- * the License. You may obtain a copy of the License at\r
- *\r
- * http://www.apache.org/licenses/LICENSE-2.0\r
- *\r
- * Unless required by applicable law or agreed to in writing, software\r
- * distributed under the License is distributed on an "AS IS" BASIS,\r
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- * See the License for the specific language governing permissions and\r
- * limitations under the License.\r
- */\r
-//--------------------------------------\r
-// XerialJ\r
-//\r
-// Base64InputStream.java\r
-// Since: 2009/02/12 16:54:54\r
-//\r
-// $URL$\r
-// $Author$\r
-//--------------------------------------\r
-package org.xerial.util.io;\r
-\r
-import java.io.FilterInputStream;\r
-import java.io.IOException;\r
-import java.io.InputStream;\r
-\r
-/**\r
- * Provides Base64 encoding and decoding in a streaming fashion (unlimited\r
- * size). When encoding the default lineLength is 76 characters and the default\r
- * lineEnding is CRLF, but these can be overridden by using the appropriate\r
- * constructor.\r
- * <p>\r
- * The default behaviour of the Base64InputStream is to DECODE, whereas the\r
- * default behaviour of the Base64OutputStream is to ENCODE, but this behaviour\r
- * can be overridden by using a different constructor.\r
- * </p>\r
- * <p>\r
- * This class implements section <cite>6.8. Base64\r
- * Content-Transfer-Encoding</cite> from RFC 2045 <cite>Multipurpose Internet\r
- * Mail Extensions (MIME) Part One: Format of Internet Message Bodies</cite> by\r
- * Freed and Borenstein.\r
- * </p>\r
- * \r
- * @author Apache Software Foundation\r
- * @version $Id $\r
- * @see <a href="http://www.ietf.org/rfc/rfc2045.txt">RFC 2045< /a>\r
- * @since 1.0-dev\r
- */\r
-public class Base64InputStream extends FilterInputStream\r
-{\r
-\r
- private final boolean doEncode;\r
- private final Base64 base64;\r
- private final byte[] singleByte = new byte[1];\r
-\r
- /**\r
- * Creates a Base64InputStream such that all data read is Base64-decoded\r
- * from the original provided InputStream.\r
- * \r
- * @param in\r
- * InputStream to wrap.\r
- */\r
- public Base64InputStream(InputStream in)\r
- {\r
- this(in, false);\r
- }\r
-\r
- /**\r
- * Creates a Base64InputStream such that all data read is either\r
- * Base64-encoded or Base64-decoded from the original provided InputStream.\r
- * \r
- * @param in\r
- * InputStream to wrap.\r
- * @param doEncode\r
- * true if we should encode all data read from us, false if we\r
- * should decode.\r
- */\r
- public Base64InputStream(InputStream in, boolean doEncode)\r
- {\r
- super(in);\r
- this.doEncode = doEncode;\r
- this.base64 = new Base64();\r
- }\r
-\r
- /**\r
- * Creates a Base64InputStream such that all data read is either\r
- * Base64-encoded or Base64-decoded from the original provided InputStream.\r
- * \r
- * @param in\r
- * InputStream to wrap.\r
- * @param doEncode\r
- * true if we should encode all data read from us, false if we\r
- * should decode.\r
- * @param lineLength\r
- * If doEncode is true, each line of encoded data will contain\r
- * lineLength characters. If lineLength <=0, the encoded data is\r
- * not divided into lines. If doEncode is false, lineLength is\r
- * ignored.\r
- * @param lineSeparator\r
- * If doEncode is true, each line of encoded data will be\r
- * terminated with this byte sequence (e.g. \r\n). If lineLength\r
- * <= 0, the lineSeparator is not used. If doEncode is false\r
- * lineSeparator is ignored.\r
- */\r
- public Base64InputStream(InputStream in, boolean doEncode, int lineLength, byte[] lineSeparator)\r
- {\r
- super(in);\r
- this.doEncode = doEncode;\r
- this.base64 = new Base64(lineLength, lineSeparator);\r
- }\r
-\r
- /**\r
- * Reads one <code>byte</code> from this input stream.\r
- * \r
- * @return the byte as an integer in the range 0 to 255 Returns -1 if EOF\r
- * has been reached.\r
- */\r
- public int read() throws IOException\r
- {\r
- int r = read(singleByte, 0, 1);\r
- while (r == 0)\r
- {\r
- r = read(singleByte, 0, 1);\r
- }\r
- if (r > 0)\r
- {\r
- return singleByte[0] < 0 ? 256 + singleByte[0] : singleByte[0];\r
- }\r
- else\r
- {\r
- return -1;\r
- }\r
- }\r
-\r
- /**\r
- * Attempts to read <code>len</code> bytes into the specified <code>b</code>\r
- * array starting at <code>offset</code> from this InputStream.\r
- * \r
- * @param b\r
- * destination byte array\r
- * @param offset\r
- * where to start writing the bytes\r
- * @param len\r
- * maximum number of bytes to read\r
- * \r
- * @return number of bytes read\r
- * @throws IOException\r
- * if an I/O error occurs.\r
- * @throws NullPointerException\r
- * if the byte array parameter is null\r
- * @throws IndexOutOfBoundsException\r
- * if offset, len or buffer size are invalid\r
- */\r
- public int read(byte b[], int offset, int len) throws IOException\r
- {\r
- if (b == null)\r
- {\r
- throw new NullPointerException();\r
- }\r
- else if (offset < 0 || len < 0 || offset + len < 0)\r
- {\r
- throw new IndexOutOfBoundsException();\r
- }\r
- else if (offset > b.length || offset + len > b.length)\r
- {\r
- throw new IndexOutOfBoundsException();\r
- }\r
- else if (len == 0)\r
- {\r
- return 0;\r
- }\r
- else\r
- {\r
- if (!base64.hasData())\r
- {\r
- byte[] buf = new byte[doEncode ? 4096 : 8192];\r
- int c = in.read(buf);\r
-\r
- // A little optimization to avoid System.arraycopy()\r
- // when possible.\r
- if (c > 0 && b.length == len)\r
- {\r
- base64.setInitialBuffer(b, offset, len);\r
- }\r
-\r
- if (doEncode)\r
- {\r
- base64.encode(buf, 0, c);\r
- }\r
- else\r
- {\r
- base64.decode(buf, 0, c);\r
- }\r
- }\r
- return base64.readResults(b, offset, len);\r
- }\r
- }\r
-\r
- /**\r
- * {@inheritDoc}\r
- * \r
- * @return false\r
- */\r
- public boolean markSupported()\r
- {\r
- return false; // not an easy job to support marks\r
- }\r
-}\r