Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/misc/CharacterDecoder.java
38829 views
/*1* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.misc;2627import java.io.OutputStream;28import java.io.ByteArrayOutputStream;29import java.io.InputStream;30import java.io.PushbackInputStream;31import java.io.ByteArrayInputStream;32import java.io.IOException;33import java.nio.ByteBuffer;3435/**36* This class defines the decoding half of character encoders.37* A character decoder is an algorithim for transforming 8 bit38* binary data that has been encoded into text by a character39* encoder, back into original binary form.40*41* The character encoders, in general, have been structured42* around a central theme that binary data can be encoded into43* text that has the form:44*45* <pre>46* [Buffer Prefix]47* [Line Prefix][encoded data atoms][Line Suffix]48* [Buffer Suffix]49* </pre>50*51* Of course in the simplest encoding schemes, the buffer has no52* distinct prefix of suffix, however all have some fixed relationship53* between the text in an 'atom' and the binary data itself.54*55* In the CharacterEncoder and CharacterDecoder classes, one complete56* chunk of data is referred to as a <i>buffer</i>. Encoded buffers57* are all text, and decoded buffers (sometimes just referred to as58* buffers) are binary octets.59*60* To create a custom decoder, you must, at a minimum, overide three61* abstract methods in this class.62* <DL>63* <DD>bytesPerAtom which tells the decoder how many bytes to64* expect from decodeAtom65* <DD>decodeAtom which decodes the bytes sent to it as text.66* <DD>bytesPerLine which tells the encoder the maximum number of67* bytes per line.68* </DL>69*70* In general, the character decoders return error in the form of a71* CEFormatException. The syntax of the detail string is72* <pre>73* DecoderClassName: Error message.74* </pre>75*76* Several useful decoders have already been written and are77* referenced in the See Also list below.78*79* @author Chuck McManis80* @see CEFormatException81* @see CharacterEncoder82* @see UCDecoder83* @see UUDecoder84* @see BASE64Decoder85*/8687public abstract class CharacterDecoder {8889/** Return the number of bytes per atom of decoding */90abstract protected int bytesPerAtom();9192/** Return the maximum number of bytes that can be encoded per line */93abstract protected int bytesPerLine();9495/** decode the beginning of the buffer, by default this is a NOP. */96protected void decodeBufferPrefix(PushbackInputStream aStream, OutputStream bStream) throws IOException { }9798/** decode the buffer suffix, again by default it is a NOP. */99protected void decodeBufferSuffix(PushbackInputStream aStream, OutputStream bStream) throws IOException { }100101/**102* This method should return, if it knows, the number of bytes103* that will be decoded. Many formats such as uuencoding provide104* this information. By default we return the maximum bytes that105* could have been encoded on the line.106*/107protected int decodeLinePrefix(PushbackInputStream aStream, OutputStream bStream) throws IOException {108return (bytesPerLine());109}110111/**112* This method post processes the line, if there are error detection113* or correction codes in a line, they are generally processed by114* this method. The simplest version of this method looks for the115* (newline) character.116*/117protected void decodeLineSuffix(PushbackInputStream aStream, OutputStream bStream) throws IOException { }118119/**120* This method does an actual decode. It takes the decoded bytes and121* writes them to the OutputStream. The integer <i>l</i> tells the122* method how many bytes are required. This is always <= bytesPerAtom().123*/124protected void decodeAtom(PushbackInputStream aStream, OutputStream bStream, int l) throws IOException {125throw new CEStreamExhausted();126}127128/**129* This method works around the bizarre semantics of BufferedInputStream's130* read method.131*/132protected int readFully(InputStream in, byte buffer[], int offset, int len)133throws java.io.IOException {134for (int i = 0; i < len; i++) {135int q = in.read();136if (q == -1)137return ((i == 0) ? -1 : i);138buffer[i+offset] = (byte)q;139}140return len;141}142143/**144* Decode the text from the InputStream and write the decoded145* octets to the OutputStream. This method runs until the stream146* is exhausted.147* @exception CEFormatException An error has occurred while decoding148* @exception CEStreamExhausted The input stream is unexpectedly out of data149*/150public void decodeBuffer(InputStream aStream, OutputStream bStream) throws IOException {151int i;152int totalBytes = 0;153154PushbackInputStream ps = new PushbackInputStream (aStream);155decodeBufferPrefix(ps, bStream);156while (true) {157int length;158159try {160length = decodeLinePrefix(ps, bStream);161for (i = 0; (i+bytesPerAtom()) < length; i += bytesPerAtom()) {162decodeAtom(ps, bStream, bytesPerAtom());163totalBytes += bytesPerAtom();164}165if ((i + bytesPerAtom()) == length) {166decodeAtom(ps, bStream, bytesPerAtom());167totalBytes += bytesPerAtom();168} else {169decodeAtom(ps, bStream, length - i);170totalBytes += (length - i);171}172decodeLineSuffix(ps, bStream);173} catch (CEStreamExhausted e) {174break;175}176}177decodeBufferSuffix(ps, bStream);178}179180/**181* Alternate decode interface that takes a String containing the encoded182* buffer and returns a byte array containing the data.183* @exception CEFormatException An error has occurred while decoding184*/185public byte decodeBuffer(String inputString)[] throws IOException {186byte inputBuffer[] = new byte[inputString.length()];187ByteArrayInputStream inStream;188ByteArrayOutputStream outStream;189190inputString.getBytes(0, inputString.length(), inputBuffer, 0);191inStream = new ByteArrayInputStream(inputBuffer);192outStream = new ByteArrayOutputStream();193decodeBuffer(inStream, outStream);194return (outStream.toByteArray());195}196197/**198* Decode the contents of the inputstream into a buffer.199*/200public byte decodeBuffer(InputStream in)[] throws IOException {201ByteArrayOutputStream outStream = new ByteArrayOutputStream();202decodeBuffer(in, outStream);203return (outStream.toByteArray());204}205206/**207* Decode the contents of the String into a ByteBuffer.208*/209public ByteBuffer decodeBufferToByteBuffer(String inputString)210throws IOException {211return ByteBuffer.wrap(decodeBuffer(inputString));212}213214/**215* Decode the contents of the inputStream into a ByteBuffer.216*/217public ByteBuffer decodeBufferToByteBuffer(InputStream in)218throws IOException {219return ByteBuffer.wrap(decodeBuffer(in));220}221}222223224