mirror of
https://github.com/openjdk/jdk.git
synced 2026-03-10 08:01:54 +00:00
8144479: Remove character coders from sun.misc
Reviewed-by: psandoz, rriggs
This commit is contained in:
parent
6853999d56
commit
bc11b9c8b5
@ -1,163 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package sun.misc;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.PushbackInputStream;
|
||||
import java.io.PrintStream;
|
||||
|
||||
/**
|
||||
* This class implements a BASE64 Character decoder as specified in RFC1521.
|
||||
*
|
||||
* This RFC is part of the MIME specification which is published by the
|
||||
* Internet Engineering Task Force (IETF). Unlike some other encoding
|
||||
* schemes there is nothing in this encoding that tells the decoder
|
||||
* where a buffer starts or stops, so to use it you will need to isolate
|
||||
* your encoded data into a single chunk and then feed them this decoder.
|
||||
* The simplest way to do that is to read all of the encoded data into a
|
||||
* string and then use:
|
||||
* <pre>
|
||||
* byte mydata[];
|
||||
* BASE64Decoder base64 = new BASE64Decoder();
|
||||
*
|
||||
* mydata = base64.decodeBuffer(bufferString);
|
||||
* </pre>
|
||||
* This will decode the String in <i>bufferString</i> and give you an array
|
||||
* of bytes in the array <i>myData</i>.
|
||||
*
|
||||
* On errors, this class throws a CEFormatException with the following detail
|
||||
* strings:
|
||||
* <pre>
|
||||
* "BASE64Decoder: Not enough bytes for an atom."
|
||||
* </pre>
|
||||
*
|
||||
* @author Chuck McManis
|
||||
* @see CharacterEncoder
|
||||
* @see BASE64Decoder
|
||||
*/
|
||||
|
||||
public class BASE64Decoder extends CharacterDecoder {
|
||||
|
||||
/** This class has 4 bytes per atom */
|
||||
protected int bytesPerAtom() {
|
||||
return (4);
|
||||
}
|
||||
|
||||
/** Any multiple of 4 will do, 72 might be common */
|
||||
protected int bytesPerLine() {
|
||||
return (72);
|
||||
}
|
||||
|
||||
/**
|
||||
* This character array provides the character to value map
|
||||
* based on RFC1521.
|
||||
*/
|
||||
private static final char pem_array[] = {
|
||||
// 0 1 2 3 4 5 6 7
|
||||
'A','B','C','D','E','F','G','H', // 0
|
||||
'I','J','K','L','M','N','O','P', // 1
|
||||
'Q','R','S','T','U','V','W','X', // 2
|
||||
'Y','Z','a','b','c','d','e','f', // 3
|
||||
'g','h','i','j','k','l','m','n', // 4
|
||||
'o','p','q','r','s','t','u','v', // 5
|
||||
'w','x','y','z','0','1','2','3', // 6
|
||||
'4','5','6','7','8','9','+','/' // 7
|
||||
};
|
||||
|
||||
private static final byte pem_convert_array[] = new byte[256];
|
||||
|
||||
static {
|
||||
for (int i = 0; i < 255; i++) {
|
||||
pem_convert_array[i] = -1;
|
||||
}
|
||||
for (int i = 0; i < pem_array.length; i++) {
|
||||
pem_convert_array[pem_array[i]] = (byte) i;
|
||||
}
|
||||
}
|
||||
|
||||
byte decode_buffer[] = new byte[4];
|
||||
|
||||
/**
|
||||
* Decode one BASE64 atom into 1, 2, or 3 bytes of data.
|
||||
*/
|
||||
@SuppressWarnings("fallthrough")
|
||||
protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int rem)
|
||||
throws java.io.IOException
|
||||
{
|
||||
int i;
|
||||
byte a = -1, b = -1, c = -1, d = -1;
|
||||
|
||||
if (rem < 2) {
|
||||
throw new CEFormatException("BASE64Decoder: Not enough bytes for an atom.");
|
||||
}
|
||||
do {
|
||||
i = inStream.read();
|
||||
if (i == -1) {
|
||||
throw new CEStreamExhausted();
|
||||
}
|
||||
} while (i == '\n' || i == '\r');
|
||||
decode_buffer[0] = (byte) i;
|
||||
|
||||
i = readFully(inStream, decode_buffer, 1, rem-1);
|
||||
if (i == -1) {
|
||||
throw new CEStreamExhausted();
|
||||
}
|
||||
|
||||
if (rem > 3 && decode_buffer[3] == '=') {
|
||||
rem = 3;
|
||||
}
|
||||
if (rem > 2 && decode_buffer[2] == '=') {
|
||||
rem = 2;
|
||||
}
|
||||
switch (rem) {
|
||||
case 4:
|
||||
d = pem_convert_array[decode_buffer[3] & 0xff];
|
||||
// NOBREAK
|
||||
case 3:
|
||||
c = pem_convert_array[decode_buffer[2] & 0xff];
|
||||
// NOBREAK
|
||||
case 2:
|
||||
b = pem_convert_array[decode_buffer[1] & 0xff];
|
||||
a = pem_convert_array[decode_buffer[0] & 0xff];
|
||||
break;
|
||||
}
|
||||
|
||||
switch (rem) {
|
||||
case 2:
|
||||
outStream.write( (byte)(((a << 2) & 0xfc) | ((b >>> 4) & 3)) );
|
||||
break;
|
||||
case 3:
|
||||
outStream.write( (byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)) );
|
||||
outStream.write( (byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)) );
|
||||
break;
|
||||
case 4:
|
||||
outStream.write( (byte) (((a << 2) & 0xfc) | ((b >>> 4) & 3)) );
|
||||
outStream.write( (byte) (((b << 4) & 0xf0) | ((c >>> 2) & 0xf)) );
|
||||
outStream.write( (byte) (((c << 6) & 0xc0) | (d & 0x3f)) );
|
||||
break;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -1,112 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 1995, 1997, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package sun.misc;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* This class implements a BASE64 Character encoder as specified in RFC1521.
|
||||
* This RFC is part of the MIME specification as published by the Internet
|
||||
* Engineering Task Force (IETF). Unlike some other encoding schemes there
|
||||
* is nothing in this encoding that indicates
|
||||
* where a buffer starts or ends.
|
||||
*
|
||||
* This means that the encoded text will simply start with the first line
|
||||
* of encoded text and end with the last line of encoded text.
|
||||
*
|
||||
* @author Chuck McManis
|
||||
* @see CharacterEncoder
|
||||
* @see BASE64Decoder
|
||||
*/
|
||||
|
||||
public class BASE64Encoder extends CharacterEncoder {
|
||||
|
||||
/** this class encodes three bytes per atom. */
|
||||
protected int bytesPerAtom() {
|
||||
return (3);
|
||||
}
|
||||
|
||||
/**
|
||||
* this class encodes 57 bytes per line. This results in a maximum
|
||||
* of 57/3 * 4 or 76 characters per output line. Not counting the
|
||||
* line termination.
|
||||
*/
|
||||
protected int bytesPerLine() {
|
||||
return (57);
|
||||
}
|
||||
|
||||
/** This array maps the characters to their 6 bit values */
|
||||
private static final char pem_array[] = {
|
||||
// 0 1 2 3 4 5 6 7
|
||||
'A','B','C','D','E','F','G','H', // 0
|
||||
'I','J','K','L','M','N','O','P', // 1
|
||||
'Q','R','S','T','U','V','W','X', // 2
|
||||
'Y','Z','a','b','c','d','e','f', // 3
|
||||
'g','h','i','j','k','l','m','n', // 4
|
||||
'o','p','q','r','s','t','u','v', // 5
|
||||
'w','x','y','z','0','1','2','3', // 6
|
||||
'4','5','6','7','8','9','+','/' // 7
|
||||
};
|
||||
|
||||
/**
|
||||
* encodeAtom - Take three bytes of input and encode it as 4
|
||||
* printable characters. Note that if the length in len is less
|
||||
* than three is encodes either one or two '=' signs to indicate
|
||||
* padding characters.
|
||||
*/
|
||||
protected void encodeAtom(OutputStream outStream, byte data[], int offset, int len)
|
||||
throws IOException {
|
||||
byte a, b, c;
|
||||
|
||||
if (len == 1) {
|
||||
a = data[offset];
|
||||
b = 0;
|
||||
c = 0;
|
||||
outStream.write(pem_array[(a >>> 2) & 0x3F]);
|
||||
outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
|
||||
outStream.write('=');
|
||||
outStream.write('=');
|
||||
} else if (len == 2) {
|
||||
a = data[offset];
|
||||
b = data[offset+1];
|
||||
c = 0;
|
||||
outStream.write(pem_array[(a >>> 2) & 0x3F]);
|
||||
outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
|
||||
outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
|
||||
outStream.write('=');
|
||||
} else {
|
||||
a = data[offset];
|
||||
b = data[offset+1];
|
||||
c = data[offset+2];
|
||||
outStream.write(pem_array[(a >>> 2) & 0x3F]);
|
||||
outStream.write(pem_array[((a << 4) & 0x30) + ((b >>> 4) & 0xf)]);
|
||||
outStream.write(pem_array[((b << 2) & 0x3c) + ((c >>> 6) & 0x3)]);
|
||||
outStream.write(pem_array[c & 0x3F]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,218 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 1995, 2013, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package sun.misc;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.PushbackInputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
/**
|
||||
* This class defines the decoding half of character encoders.
|
||||
* A character decoder is an algorithim for transforming 8 bit
|
||||
* binary data that has been encoded into text by a character
|
||||
* encoder, back into original binary form.
|
||||
*
|
||||
* The character encoders, in general, have been structured
|
||||
* around a central theme that binary data can be encoded into
|
||||
* text that has the form:
|
||||
*
|
||||
* <pre>
|
||||
* [Buffer Prefix]
|
||||
* [Line Prefix][encoded data atoms][Line Suffix]
|
||||
* [Buffer Suffix]
|
||||
* </pre>
|
||||
*
|
||||
* Of course in the simplest encoding schemes, the buffer has no
|
||||
* distinct prefix of suffix, however all have some fixed relationship
|
||||
* between the text in an 'atom' and the binary data itself.
|
||||
*
|
||||
* In the CharacterEncoder and CharacterDecoder classes, one complete
|
||||
* chunk of data is referred to as a <i>buffer</i>. Encoded buffers
|
||||
* are all text, and decoded buffers (sometimes just referred to as
|
||||
* buffers) are binary octets.
|
||||
*
|
||||
* To create a custom decoder, you must, at a minimum, overide three
|
||||
* abstract methods in this class.
|
||||
* <DL>
|
||||
* <DD>bytesPerAtom which tells the decoder how many bytes to
|
||||
* expect from decodeAtom
|
||||
* <DD>decodeAtom which decodes the bytes sent to it as text.
|
||||
* <DD>bytesPerLine which tells the encoder the maximum number of
|
||||
* bytes per line.
|
||||
* </DL>
|
||||
*
|
||||
* In general, the character decoders return error in the form of a
|
||||
* CEFormatException. The syntax of the detail string is
|
||||
* <pre>
|
||||
* DecoderClassName: Error message.
|
||||
* </pre>
|
||||
*
|
||||
* Several useful decoders have already been written and are
|
||||
* referenced in the See Also list below.
|
||||
*
|
||||
* @author Chuck McManis
|
||||
* @see CEFormatException
|
||||
* @see CharacterEncoder
|
||||
* @see UCDecoder
|
||||
* @see UUDecoder
|
||||
* @see BASE64Decoder
|
||||
*/
|
||||
|
||||
public abstract class CharacterDecoder {
|
||||
|
||||
/** Return the number of bytes per atom of decoding */
|
||||
protected abstract int bytesPerAtom();
|
||||
|
||||
/** Return the maximum number of bytes that can be encoded per line */
|
||||
protected abstract int bytesPerLine();
|
||||
|
||||
/** decode the beginning of the buffer, by default this is a NOP. */
|
||||
protected void decodeBufferPrefix(PushbackInputStream aStream, OutputStream bStream) throws IOException { }
|
||||
|
||||
/** decode the buffer suffix, again by default it is a NOP. */
|
||||
protected void decodeBufferSuffix(PushbackInputStream aStream, OutputStream bStream) throws IOException { }
|
||||
|
||||
/**
|
||||
* This method should return, if it knows, the number of bytes
|
||||
* that will be decoded. Many formats such as uuencoding provide
|
||||
* this information. By default we return the maximum bytes that
|
||||
* could have been encoded on the line.
|
||||
*/
|
||||
protected int decodeLinePrefix(PushbackInputStream aStream, OutputStream bStream) throws IOException {
|
||||
return (bytesPerLine());
|
||||
}
|
||||
|
||||
/**
|
||||
* This method post processes the line, if there are error detection
|
||||
* or correction codes in a line, they are generally processed by
|
||||
* this method. The simplest version of this method looks for the
|
||||
* (newline) character.
|
||||
*/
|
||||
protected void decodeLineSuffix(PushbackInputStream aStream, OutputStream bStream) throws IOException { }
|
||||
|
||||
/**
|
||||
* This method does an actual decode. It takes the decoded bytes and
|
||||
* writes them to the OutputStream. The integer <i>l</i> tells the
|
||||
* method how many bytes are required. This is always {@literal <=} bytesPerAtom().
|
||||
*/
|
||||
protected void decodeAtom(PushbackInputStream aStream, OutputStream bStream, int l) throws IOException {
|
||||
throw new CEStreamExhausted();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method works around the bizarre semantics of BufferedInputStream's
|
||||
* read method.
|
||||
*/
|
||||
protected int readFully(InputStream in, byte buffer[], int offset, int len)
|
||||
throws java.io.IOException {
|
||||
for (int i = 0; i < len; i++) {
|
||||
int q = in.read();
|
||||
if (q == -1)
|
||||
return ((i == 0) ? -1 : i);
|
||||
buffer[i+offset] = (byte)q;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode the text from the InputStream and write the decoded
|
||||
* octets to the OutputStream. This method runs until the stream
|
||||
* is exhausted.
|
||||
* @exception CEFormatException An error has occurred while decoding
|
||||
* @exception CEStreamExhausted The input stream is unexpectedly out of data
|
||||
*/
|
||||
public void decodeBuffer(InputStream aStream, OutputStream bStream) throws IOException {
|
||||
int i;
|
||||
int totalBytes = 0;
|
||||
|
||||
PushbackInputStream ps = new PushbackInputStream (aStream);
|
||||
decodeBufferPrefix(ps, bStream);
|
||||
while (true) {
|
||||
int length;
|
||||
|
||||
try {
|
||||
length = decodeLinePrefix(ps, bStream);
|
||||
for (i = 0; (i+bytesPerAtom()) < length; i += bytesPerAtom()) {
|
||||
decodeAtom(ps, bStream, bytesPerAtom());
|
||||
totalBytes += bytesPerAtom();
|
||||
}
|
||||
if ((i + bytesPerAtom()) == length) {
|
||||
decodeAtom(ps, bStream, bytesPerAtom());
|
||||
totalBytes += bytesPerAtom();
|
||||
} else {
|
||||
decodeAtom(ps, bStream, length - i);
|
||||
totalBytes += (length - i);
|
||||
}
|
||||
decodeLineSuffix(ps, bStream);
|
||||
} catch (CEStreamExhausted e) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
decodeBufferSuffix(ps, bStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Alternate decode interface that takes a String containing the encoded
|
||||
* buffer and returns a byte array containing the data.
|
||||
* @exception CEFormatException An error has occurred while decoding
|
||||
*/
|
||||
public byte[] decodeBuffer(String inputString) throws IOException {
|
||||
byte inputBuffer[] = inputString.getBytes("ISO-8859-1");
|
||||
ByteArrayInputStream inStream = new ByteArrayInputStream(inputBuffer);
|
||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||
decodeBuffer(inStream, outStream);
|
||||
return outStream.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode the contents of the inputstream into a buffer.
|
||||
*/
|
||||
public byte[] decodeBuffer(InputStream in) throws IOException {
|
||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||
decodeBuffer(in, outStream);
|
||||
return outStream.toByteArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode the contents of the String into a ByteBuffer.
|
||||
*/
|
||||
public ByteBuffer decodeBufferToByteBuffer(String inputString)
|
||||
throws IOException {
|
||||
return ByteBuffer.wrap(decodeBuffer(inputString));
|
||||
}
|
||||
|
||||
/**
|
||||
* Decode the contents of the inputStream into a ByteBuffer.
|
||||
*/
|
||||
public ByteBuffer decodeBufferToByteBuffer(InputStream in)
|
||||
throws IOException {
|
||||
return ByteBuffer.wrap(decodeBuffer(in));
|
||||
}
|
||||
}
|
||||
@ -1,354 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 1995, 2005, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
package sun.misc;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
|
||||
|
||||
/**
|
||||
* This class defines the encoding half of character encoders.
|
||||
* A character encoder is an algorithim for transforming 8 bit binary
|
||||
* data into text (generally 7 bit ASCII or 8 bit ISO-Latin-1 text)
|
||||
* for transmition over text channels such as e-mail and network news.
|
||||
*
|
||||
* The character encoders have been structured around a central theme
|
||||
* that, in general, the encoded text has the form:
|
||||
*
|
||||
* <pre>
|
||||
* [Buffer Prefix]
|
||||
* [Line Prefix][encoded data atoms][Line Suffix]
|
||||
* [Buffer Suffix]
|
||||
* </pre>
|
||||
*
|
||||
* In the CharacterEncoder and CharacterDecoder classes, one complete
|
||||
* chunk of data is referred to as a <i>buffer</i>. Encoded buffers
|
||||
* are all text, and decoded buffers (sometimes just referred to as
|
||||
* buffers) are binary octets.
|
||||
*
|
||||
* To create a custom encoder, you must, at a minimum, overide three
|
||||
* abstract methods in this class.
|
||||
* <DL>
|
||||
* <DD>bytesPerAtom which tells the encoder how many bytes to
|
||||
* send to encodeAtom
|
||||
* <DD>encodeAtom which encodes the bytes sent to it as text.
|
||||
* <DD>bytesPerLine which tells the encoder the maximum number of
|
||||
* bytes per line.
|
||||
* </DL>
|
||||
*
|
||||
* Several useful encoders have already been written and are
|
||||
* referenced in the See Also list below.
|
||||
*
|
||||
* @author Chuck McManis
|
||||
* @see CharacterDecoder
|
||||
* @see UCEncoder
|
||||
* @see UUEncoder
|
||||
* @see BASE64Encoder
|
||||
*/
|
||||
public abstract class CharacterEncoder {
|
||||
|
||||
/** Stream that understands "printing" */
|
||||
protected PrintStream pStream;
|
||||
|
||||
/** Return the number of bytes per atom of encoding */
|
||||
protected abstract int bytesPerAtom();
|
||||
|
||||
/** Return the number of bytes that can be encoded per line */
|
||||
protected abstract int bytesPerLine();
|
||||
|
||||
/**
|
||||
* Encode the prefix for the entire buffer. By default is simply
|
||||
* opens the PrintStream for use by the other functions.
|
||||
*/
|
||||
protected void encodeBufferPrefix(OutputStream aStream) throws IOException {
|
||||
pStream = new PrintStream(aStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the suffix for the entire buffer.
|
||||
*/
|
||||
protected void encodeBufferSuffix(OutputStream aStream) throws IOException {
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the prefix that starts every output line.
|
||||
*/
|
||||
protected void encodeLinePrefix(OutputStream aStream, int aLength)
|
||||
throws IOException {
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the suffix that ends every output line. By default
|
||||
* this method just prints a newline into the output stream.
|
||||
*/
|
||||
protected void encodeLineSuffix(OutputStream aStream) throws IOException {
|
||||
pStream.println();
|
||||
}
|
||||
|
||||
/** Encode one "atom" of information into characters. */
|
||||
protected abstract void encodeAtom(OutputStream aStream, byte someBytes[],
|
||||
int anOffset, int aLength) throws IOException;
|
||||
|
||||
/**
|
||||
* This method works around the bizarre semantics of BufferedInputStream's
|
||||
* read method.
|
||||
*/
|
||||
protected int readFully(InputStream in, byte buffer[])
|
||||
throws java.io.IOException {
|
||||
for (int i = 0; i < buffer.length; i++) {
|
||||
int q = in.read();
|
||||
if (q == -1)
|
||||
return i;
|
||||
buffer[i] = (byte)q;
|
||||
}
|
||||
return buffer.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode bytes from the input stream, and write them as text characters
|
||||
* to the output stream. This method will run until it exhausts the
|
||||
* input stream, but does not print the line suffix for a final
|
||||
* line that is shorter than bytesPerLine().
|
||||
*/
|
||||
public void encode(InputStream inStream, OutputStream outStream)
|
||||
throws IOException {
|
||||
int j;
|
||||
int numBytes;
|
||||
byte tmpbuffer[] = new byte[bytesPerLine()];
|
||||
|
||||
encodeBufferPrefix(outStream);
|
||||
|
||||
while (true) {
|
||||
numBytes = readFully(inStream, tmpbuffer);
|
||||
if (numBytes == 0) {
|
||||
break;
|
||||
}
|
||||
encodeLinePrefix(outStream, numBytes);
|
||||
for (j = 0; j < numBytes; j += bytesPerAtom()) {
|
||||
|
||||
if ((j + bytesPerAtom()) <= numBytes) {
|
||||
encodeAtom(outStream, tmpbuffer, j, bytesPerAtom());
|
||||
} else {
|
||||
encodeAtom(outStream, tmpbuffer, j, (numBytes)- j);
|
||||
}
|
||||
}
|
||||
if (numBytes < bytesPerLine()) {
|
||||
break;
|
||||
} else {
|
||||
encodeLineSuffix(outStream);
|
||||
}
|
||||
}
|
||||
encodeBufferSuffix(outStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the buffer in <i>aBuffer</i> and write the encoded
|
||||
* result to the OutputStream <i>aStream</i>.
|
||||
*/
|
||||
public void encode(byte aBuffer[], OutputStream aStream)
|
||||
throws IOException {
|
||||
ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
|
||||
encode(inStream, aStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* A 'streamless' version of encode that simply takes a buffer of
|
||||
* bytes and returns a string containing the encoded buffer.
|
||||
*/
|
||||
public String encode(byte aBuffer[]) {
|
||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||
ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
|
||||
String retVal = null;
|
||||
try {
|
||||
encode(inStream, outStream);
|
||||
// explicit ascii->unicode conversion
|
||||
retVal = outStream.toString("ISO-8859-1");
|
||||
} catch (Exception IOException) {
|
||||
// This should never happen.
|
||||
throw new Error("CharacterEncoder.encode internal error");
|
||||
}
|
||||
return (retVal);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a byte array from the remaining bytes in this ByteBuffer.
|
||||
* <P>
|
||||
* The ByteBuffer's position will be advanced to ByteBuffer's limit.
|
||||
* <P>
|
||||
* To avoid an extra copy, the implementation will attempt to return the
|
||||
* byte array backing the ByteBuffer. If this is not possible, a
|
||||
* new byte array will be created.
|
||||
*/
|
||||
private byte [] getBytes(ByteBuffer bb) {
|
||||
/*
|
||||
* This should never return a BufferOverflowException, as we're
|
||||
* careful to allocate just the right amount.
|
||||
*/
|
||||
byte [] buf = null;
|
||||
|
||||
/*
|
||||
* If it has a usable backing byte buffer, use it. Use only
|
||||
* if the array exactly represents the current ByteBuffer.
|
||||
*/
|
||||
if (bb.hasArray()) {
|
||||
byte [] tmp = bb.array();
|
||||
if ((tmp.length == bb.capacity()) &&
|
||||
(tmp.length == bb.remaining())) {
|
||||
buf = tmp;
|
||||
bb.position(bb.limit());
|
||||
}
|
||||
}
|
||||
|
||||
if (buf == null) {
|
||||
/*
|
||||
* This class doesn't have a concept of encode(buf, len, off),
|
||||
* so if we have a partial buffer, we must reallocate
|
||||
* space.
|
||||
*/
|
||||
buf = new byte[bb.remaining()];
|
||||
|
||||
/*
|
||||
* position() automatically updated
|
||||
*/
|
||||
bb.get(buf);
|
||||
}
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the <i>aBuffer</i> ByteBuffer and write the encoded
|
||||
* result to the OutputStream <i>aStream</i>.
|
||||
* <P>
|
||||
* The ByteBuffer's position will be advanced to ByteBuffer's limit.
|
||||
*/
|
||||
public void encode(ByteBuffer aBuffer, OutputStream aStream)
|
||||
throws IOException {
|
||||
byte [] buf = getBytes(aBuffer);
|
||||
encode(buf, aStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* A 'streamless' version of encode that simply takes a ByteBuffer
|
||||
* and returns a string containing the encoded buffer.
|
||||
* <P>
|
||||
* The ByteBuffer's position will be advanced to ByteBuffer's limit.
|
||||
*/
|
||||
public String encode(ByteBuffer aBuffer) {
|
||||
byte [] buf = getBytes(aBuffer);
|
||||
return encode(buf);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode bytes from the input stream, and write them as text characters
|
||||
* to the output stream. This method will run until it exhausts the
|
||||
* input stream. It differs from encode in that it will add the
|
||||
* line at the end of a final line that is shorter than bytesPerLine().
|
||||
*/
|
||||
public void encodeBuffer(InputStream inStream, OutputStream outStream)
|
||||
throws IOException {
|
||||
int j;
|
||||
int numBytes;
|
||||
byte tmpbuffer[] = new byte[bytesPerLine()];
|
||||
|
||||
encodeBufferPrefix(outStream);
|
||||
|
||||
while (true) {
|
||||
numBytes = readFully(inStream, tmpbuffer);
|
||||
if (numBytes == 0) {
|
||||
break;
|
||||
}
|
||||
encodeLinePrefix(outStream, numBytes);
|
||||
for (j = 0; j < numBytes; j += bytesPerAtom()) {
|
||||
if ((j + bytesPerAtom()) <= numBytes) {
|
||||
encodeAtom(outStream, tmpbuffer, j, bytesPerAtom());
|
||||
} else {
|
||||
encodeAtom(outStream, tmpbuffer, j, (numBytes)- j);
|
||||
}
|
||||
}
|
||||
encodeLineSuffix(outStream);
|
||||
if (numBytes < bytesPerLine()) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
encodeBufferSuffix(outStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the buffer in <i>aBuffer</i> and write the encoded
|
||||
* result to the OutputStream <i>aStream</i>.
|
||||
*/
|
||||
public void encodeBuffer(byte aBuffer[], OutputStream aStream)
|
||||
throws IOException {
|
||||
ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
|
||||
encodeBuffer(inStream, aStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* A 'streamless' version of encode that simply takes a buffer of
|
||||
* bytes and returns a string containing the encoded buffer.
|
||||
*/
|
||||
public String encodeBuffer(byte aBuffer[]) {
|
||||
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||
ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
|
||||
try {
|
||||
encodeBuffer(inStream, outStream);
|
||||
} catch (Exception IOException) {
|
||||
// This should never happen.
|
||||
throw new Error("CharacterEncoder.encodeBuffer internal error");
|
||||
}
|
||||
return (outStream.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the <i>aBuffer</i> ByteBuffer and write the encoded
|
||||
* result to the OutputStream <i>aStream</i>.
|
||||
* <P>
|
||||
* The ByteBuffer's position will be advanced to ByteBuffer's limit.
|
||||
*/
|
||||
public void encodeBuffer(ByteBuffer aBuffer, OutputStream aStream)
|
||||
throws IOException {
|
||||
byte [] buf = getBytes(aBuffer);
|
||||
encodeBuffer(buf, aStream);
|
||||
}
|
||||
|
||||
/**
|
||||
* A 'streamless' version of encode that simply takes a ByteBuffer
|
||||
* and returns a string containing the encoded buffer.
|
||||
* <P>
|
||||
* The ByteBuffer's position will be advanced to ByteBuffer's limit.
|
||||
*/
|
||||
public String encodeBuffer(ByteBuffer aBuffer) {
|
||||
byte [] buf = getBytes(aBuffer);
|
||||
return encodeBuffer(buf);
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,233 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 1995, 2000, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package sun.misc;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PushbackInputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* This class implements a robust character decoder. The decoder will
|
||||
* converted encoded text into binary data.
|
||||
*
|
||||
* The basic encoding unit is a 3 character atom. It encodes two bytes
|
||||
* of data. Bytes are encoded into a 64 character set, the characters
|
||||
* were chosen specifically because they appear in all codesets.
|
||||
* We don't care what their numerical equivalent is because
|
||||
* we use a character array to map them. This is like UUencoding
|
||||
* with the dependency on ASCII removed.
|
||||
*
|
||||
* The three chars that make up an atom are encoded as follows:
|
||||
* <pre>
|
||||
* 00xxxyyy 00axxxxx 00byyyyy
|
||||
* 00 = leading zeros, all values are 0 - 63
|
||||
* xxxyyy - Top 3 bits of X, Top 3 bits of Y
|
||||
* axxxxx - a = X parity bit, xxxxx lower 5 bits of X
|
||||
* byyyyy - b = Y parity bit, yyyyy lower 5 bits of Y
|
||||
* </pre>
|
||||
*
|
||||
* The atoms are arranged into lines suitable for inclusion into an
|
||||
* email message or text file. The number of bytes that are encoded
|
||||
* per line is 48 which keeps the total line length under 80 chars)
|
||||
*
|
||||
* Each line has the form(
|
||||
* <pre>
|
||||
* *(LLSS)(DDDD)(DDDD)(DDDD)...(CRC)
|
||||
* Where each (xxx) represents a three character atom.
|
||||
* (LLSS) - 8 bit length (high byte), and sequence number
|
||||
* modulo 256;
|
||||
* (DDDD) - Data byte atoms, if length is odd, last data
|
||||
* atom has (DD00) (high byte data, low byte 0)
|
||||
* (CRC) - 16 bit CRC for the line, includes length,
|
||||
* sequence, and all data bytes. If there is a
|
||||
* zero pad byte (odd length) it is _NOT_
|
||||
* included in the CRC.
|
||||
* </pre>
|
||||
*
|
||||
* If an error is encountered during decoding this class throws a
|
||||
* CEFormatException. The specific detail messages are:
|
||||
*
|
||||
* <pre>
|
||||
* "UCDecoder: High byte parity error."
|
||||
* "UCDecoder: Low byte parity error."
|
||||
* "UCDecoder: Out of sequence line."
|
||||
* "UCDecoder: CRC check failed."
|
||||
* </pre>
|
||||
*
|
||||
* @author Chuck McManis
|
||||
* @see CharacterEncoder
|
||||
* @see UCEncoder
|
||||
*/
|
||||
public class UCDecoder extends CharacterDecoder {
|
||||
|
||||
/** This class encodes two bytes per atom. */
|
||||
protected int bytesPerAtom() {
|
||||
return (2);
|
||||
}
|
||||
|
||||
/** this class encodes 48 bytes per line */
|
||||
protected int bytesPerLine() {
|
||||
return (48);
|
||||
}
|
||||
|
||||
/* this is the UCE mapping of 0-63 to characters .. */
|
||||
private static final byte map_array[] = {
|
||||
// 0 1 2 3 4 5 6 7
|
||||
(byte)'0',(byte)'1',(byte)'2',(byte)'3',(byte)'4',(byte)'5',(byte)'6',(byte)'7', // 0
|
||||
(byte)'8',(byte)'9',(byte)'A',(byte)'B',(byte)'C',(byte)'D',(byte)'E',(byte)'F', // 1
|
||||
(byte)'G',(byte)'H',(byte)'I',(byte)'J',(byte)'K',(byte)'L',(byte)'M',(byte)'N', // 2
|
||||
(byte)'O',(byte)'P',(byte)'Q',(byte)'R',(byte)'S',(byte)'T',(byte)'U',(byte)'V', // 3
|
||||
(byte)'W',(byte)'X',(byte)'Y',(byte)'Z',(byte)'a',(byte)'b',(byte)'c',(byte)'d', // 4
|
||||
(byte)'e',(byte)'f',(byte)'g',(byte)'h',(byte)'i',(byte)'j',(byte)'k',(byte)'l', // 5
|
||||
(byte)'m',(byte)'n',(byte)'o',(byte)'p',(byte)'q',(byte)'r',(byte)'s',(byte)'t', // 6
|
||||
(byte)'u',(byte)'v',(byte)'w',(byte)'x',(byte)'y',(byte)'z',(byte)'(',(byte)')' // 7
|
||||
};
|
||||
|
||||
private int sequence;
|
||||
private byte tmp[] = new byte[2];
|
||||
private CRC16 crc = new CRC16();
|
||||
|
||||
/**
|
||||
* Decode one atom - reads the characters from the input stream, decodes
|
||||
* them, and checks for valid parity.
|
||||
*/
|
||||
protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int l) throws IOException {
|
||||
int i, p1, p2, np1, np2;
|
||||
byte a = -1, b = -1, c = -1;
|
||||
byte high_byte, low_byte;
|
||||
byte tmp[] = new byte[3];
|
||||
|
||||
i = inStream.read(tmp);
|
||||
if (i != 3) {
|
||||
throw new CEStreamExhausted();
|
||||
}
|
||||
for (i = 0; (i < 64) && ((a == -1) || (b == -1) || (c == -1)); i++) {
|
||||
if (tmp[0] == map_array[i]) {
|
||||
a = (byte) i;
|
||||
}
|
||||
if (tmp[1] == map_array[i]) {
|
||||
b = (byte) i;
|
||||
}
|
||||
if (tmp[2] == map_array[i]) {
|
||||
c = (byte) i;
|
||||
}
|
||||
}
|
||||
high_byte = (byte) (((a & 0x38) << 2) + (b & 0x1f));
|
||||
low_byte = (byte) (((a & 0x7) << 5) + (c & 0x1f));
|
||||
p1 = 0;
|
||||
p2 = 0;
|
||||
for (i = 1; i < 256; i = i * 2) {
|
||||
if ((high_byte & i) != 0)
|
||||
p1++;
|
||||
if ((low_byte & i) != 0)
|
||||
p2++;
|
||||
}
|
||||
np1 = (b & 32) / 32;
|
||||
np2 = (c & 32) / 32;
|
||||
if ((p1 & 1) != np1) {
|
||||
throw new CEFormatException("UCDecoder: High byte parity error.");
|
||||
}
|
||||
if ((p2 & 1) != np2) {
|
||||
throw new CEFormatException("UCDecoder: Low byte parity error.");
|
||||
}
|
||||
outStream.write(high_byte);
|
||||
crc.update(high_byte);
|
||||
if (l == 2) {
|
||||
outStream.write(low_byte);
|
||||
crc.update(low_byte);
|
||||
}
|
||||
}
|
||||
|
||||
private ByteArrayOutputStream lineAndSeq = new ByteArrayOutputStream(2);
|
||||
|
||||
/**
|
||||
* decodeBufferPrefix initializes the sequence number to zero.
|
||||
*/
|
||||
protected void decodeBufferPrefix(PushbackInputStream inStream, OutputStream outStream) {
|
||||
sequence = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* decodeLinePrefix reads the sequence number and the number of
|
||||
* encoded bytes from the line. If the sequence number is not the
|
||||
* previous sequence number + 1 then an exception is thrown.
|
||||
* UCE lines are line terminator immune, they all start with *
|
||||
* so the other thing this method does is scan for the next line
|
||||
* by looking for the * character.
|
||||
*
|
||||
* @exception CEFormatException out of sequence lines detected.
|
||||
*/
|
||||
protected int decodeLinePrefix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
|
||||
int i;
|
||||
int nLen, nSeq;
|
||||
byte xtmp[];
|
||||
int c;
|
||||
|
||||
crc.value = 0;
|
||||
while (true) {
|
||||
c = inStream.read(tmp, 0, 1);
|
||||
if (c == -1) {
|
||||
throw new CEStreamExhausted();
|
||||
}
|
||||
if (tmp[0] == '*') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
lineAndSeq.reset();
|
||||
decodeAtom(inStream, lineAndSeq, 2);
|
||||
xtmp = lineAndSeq.toByteArray();
|
||||
nLen = xtmp[0] & 0xff;
|
||||
nSeq = xtmp[1] & 0xff;
|
||||
if (nSeq != sequence) {
|
||||
throw new CEFormatException("UCDecoder: Out of sequence line.");
|
||||
}
|
||||
sequence = (sequence + 1) & 0xff;
|
||||
return (nLen);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* this method reads the CRC that is at the end of every line and
|
||||
* verifies that it matches the computed CRC.
|
||||
*
|
||||
* @exception CEFormatException if CRC check fails.
|
||||
*/
|
||||
protected void decodeLineSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
|
||||
int i;
|
||||
int lineCRC = crc.value;
|
||||
int readCRC;
|
||||
byte tmp[];
|
||||
|
||||
lineAndSeq.reset();
|
||||
decodeAtom(inStream, lineAndSeq, 2);
|
||||
tmp = lineAndSeq.toByteArray();
|
||||
readCRC = ((tmp[0] << 8) & 0xFF00) + (tmp[1] & 0xff);
|
||||
if (readCRC != lineCRC) {
|
||||
throw new CEFormatException("UCDecoder: CRC check failed.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,178 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 1995, 1997, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package sun.misc;
|
||||
|
||||
import java.io.OutputStream;
|
||||
import java.io.InputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* This class implements a robust character encoder. The encoder is designed
|
||||
* to convert binary data into printable characters. The characters are
|
||||
* assumed to exist but they are not assumed to be ASCII, the complete set
|
||||
* is 0-9, A-Z, a-z, "(", and ")".
|
||||
*
|
||||
* The basic encoding unit is a 3 character atom. It encodes two bytes
|
||||
* of data. Bytes are encoded into a 64 character set, the characters
|
||||
* were chosen specifically because they appear in all codesets.
|
||||
* We don't care what their numerical equivalent is because
|
||||
* we use a character array to map them. This is like UUencoding
|
||||
* with the dependency on ASCII removed.
|
||||
*
|
||||
* The three chars that make up an atom are encoded as follows:
|
||||
* <pre>
|
||||
* 00xxxyyy 00axxxxx 00byyyyy
|
||||
* 00 = leading zeros, all values are 0 - 63
|
||||
* xxxyyy - Top 3 bits of X, Top 3 bits of Y
|
||||
* axxxxx - a = X parity bit, xxxxx lower 5 bits of X
|
||||
* byyyyy - b = Y parity bit, yyyyy lower 5 bits of Y
|
||||
* </pre>
|
||||
*
|
||||
* The atoms are arranged into lines suitable for inclusion into an
|
||||
* email message or text file. The number of bytes that are encoded
|
||||
* per line is 48 which keeps the total line length under 80 chars)
|
||||
*
|
||||
* Each line has the form(
|
||||
* <pre>
|
||||
* *(LLSS)(DDDD)(DDDD)(DDDD)...(CRC)
|
||||
* Where each (xxx) represents a three character atom.
|
||||
* (LLSS) - 8 bit length (high byte), and sequence number
|
||||
* modulo 256;
|
||||
* (DDDD) - Data byte atoms, if length is odd, last data
|
||||
* atom has (DD00) (high byte data, low byte 0)
|
||||
* (CRC) - 16 bit CRC for the line, includes length,
|
||||
* sequence, and all data bytes. If there is a
|
||||
* zero pad byte (odd length) it is _NOT_
|
||||
* included in the CRC.
|
||||
* </pre>
|
||||
*
|
||||
* @author Chuck McManis
|
||||
* @see CharacterEncoder
|
||||
* @see UCDecoder
|
||||
*/
|
||||
public class UCEncoder extends CharacterEncoder {
|
||||
|
||||
/** this clase encodes two bytes per atom */
|
||||
protected int bytesPerAtom() {
|
||||
return (2);
|
||||
}
|
||||
|
||||
/** this class encodes 48 bytes per line */
|
||||
protected int bytesPerLine() {
|
||||
return (48);
|
||||
}
|
||||
|
||||
/* this is the UCE mapping of 0-63 to characters .. */
|
||||
private static final byte map_array[] = {
|
||||
// 0 1 2 3 4 5 6 7
|
||||
(byte)'0',(byte)'1',(byte)'2',(byte)'3',(byte)'4',(byte)'5',(byte)'6',(byte)'7', // 0
|
||||
(byte)'8',(byte)'9',(byte)'A',(byte)'B',(byte)'C',(byte)'D',(byte)'E',(byte)'F', // 1
|
||||
(byte)'G',(byte)'H',(byte)'I',(byte)'J',(byte)'K',(byte)'L',(byte)'M',(byte)'N', // 2
|
||||
(byte)'O',(byte)'P',(byte)'Q',(byte)'R',(byte)'S',(byte)'T',(byte)'U',(byte)'V', // 3
|
||||
(byte)'W',(byte)'X',(byte)'Y',(byte)'Z',(byte)'a',(byte)'b',(byte)'c',(byte)'d', // 4
|
||||
(byte)'e',(byte)'f',(byte)'g',(byte)'h',(byte)'i',(byte)'j',(byte)'k',(byte)'l', // 5
|
||||
(byte)'m',(byte)'n',(byte)'o',(byte)'p',(byte)'q',(byte)'r',(byte)'s',(byte)'t', // 6
|
||||
(byte)'u',(byte)'v',(byte)'w',(byte)'x',(byte)'y',(byte)'z',(byte)'(',(byte)')' // 7
|
||||
};
|
||||
|
||||
private int sequence;
|
||||
private byte tmp[] = new byte[2];
|
||||
private CRC16 crc = new CRC16();
|
||||
|
||||
/**
|
||||
* encodeAtom - take two bytes and encode them into the correct
|
||||
* three characters. If only one byte is to be encoded, the other
|
||||
* must be zero. The padding byte is not included in the CRC computation.
|
||||
*/
|
||||
protected void encodeAtom(OutputStream outStream, byte data[], int offset, int len) throws IOException
|
||||
{
|
||||
int i;
|
||||
int p1, p2; // parity bits
|
||||
byte a, b;
|
||||
|
||||
a = data[offset];
|
||||
if (len == 2) {
|
||||
b = data[offset+1];
|
||||
} else {
|
||||
b = 0;
|
||||
}
|
||||
crc.update(a);
|
||||
if (len == 2) {
|
||||
crc.update(b);
|
||||
}
|
||||
outStream.write(map_array[((a >>> 2) & 0x38) + ((b >>> 5) & 0x7)]);
|
||||
p1 = 0; p2 = 0;
|
||||
for (i = 1; i < 256; i = i * 2) {
|
||||
if ((a & i) != 0) {
|
||||
p1++;
|
||||
}
|
||||
if ((b & i) != 0) {
|
||||
p2++;
|
||||
}
|
||||
}
|
||||
p1 = (p1 & 1) * 32;
|
||||
p2 = (p2 & 1) * 32;
|
||||
outStream.write(map_array[(a & 31) + p1]);
|
||||
outStream.write(map_array[(b & 31) + p2]);
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Each UCE encoded line starts with a prefix of '*[XXX]', where
|
||||
* the sequence number and the length are encoded in the first
|
||||
* atom.
|
||||
*/
|
||||
protected void encodeLinePrefix(OutputStream outStream, int length) throws IOException {
|
||||
outStream.write('*');
|
||||
crc.value = 0;
|
||||
tmp[0] = (byte) length;
|
||||
tmp[1] = (byte) sequence;
|
||||
sequence = (sequence + 1) & 0xff;
|
||||
encodeAtom(outStream, tmp, 0, 2);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* each UCE encoded line ends with YYY and encoded version of the
|
||||
* 16 bit checksum. The most significant byte of the check sum
|
||||
* is always encoded FIRST.
|
||||
*/
|
||||
protected void encodeLineSuffix(OutputStream outStream) throws IOException {
|
||||
tmp[0] = (byte) ((crc.value >>> 8) & 0xff);
|
||||
tmp[1] = (byte) (crc.value & 0xff);
|
||||
encodeAtom(outStream, tmp, 0, 2);
|
||||
super.pStream.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* The buffer prefix code is used to initialize the sequence number
|
||||
* to zero.
|
||||
*/
|
||||
protected void encodeBufferPrefix(OutputStream a) throws IOException {
|
||||
sequence = 0;
|
||||
super.encodeBufferPrefix(a);
|
||||
}
|
||||
}
|
||||
@ -1,274 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 1995, 2001, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package sun.misc;
|
||||
|
||||
import java.io.PushbackInputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* This class implements a Berkeley uu character decoder. This decoder
|
||||
* was made famous by the uudecode program.
|
||||
*
|
||||
* The basic character coding is algorithmic, taking 6 bits of binary
|
||||
* data and adding it to an ASCII ' ' (space) character. This converts
|
||||
* these six bits into a printable representation. Note that it depends
|
||||
* on the ASCII character encoding standard for english. Groups of three
|
||||
* bytes are converted into 4 characters by treating the three bytes
|
||||
* a four 6 bit groups, group 1 is byte 1's most significant six bits,
|
||||
* group 2 is byte 1's least significant two bits plus byte 2's four
|
||||
* most significant bits. etc.
|
||||
*
|
||||
* In this encoding, the buffer prefix is:
|
||||
* <pre>
|
||||
* begin [mode] [filename]
|
||||
* </pre>
|
||||
*
|
||||
* This is followed by one or more lines of the form:
|
||||
* <pre>
|
||||
* (len)(data)(data)(data) ...
|
||||
* </pre>
|
||||
* where (len) is the number of bytes on this line. Note that groupings
|
||||
* are always four characters, even if length is not a multiple of three
|
||||
* bytes. When less than three characters are encoded, the values of the
|
||||
* last remaining bytes is undefined and should be ignored.
|
||||
*
|
||||
* The last line of data in a uuencoded buffer is represented by a single
|
||||
* space character. This is translated by the decoding engine to a line
|
||||
* length of zero. This is immediately followed by a line which contains
|
||||
* the word 'end[newline]'
|
||||
*
|
||||
* If an error is encountered during decoding this class throws a
|
||||
* CEFormatException. The specific detail messages are:
|
||||
*
|
||||
* <pre>
|
||||
* "UUDecoder: No begin line."
|
||||
* "UUDecoder: Malformed begin line."
|
||||
* "UUDecoder: Short Buffer."
|
||||
* "UUDecoder: Bad Line Length."
|
||||
* "UUDecoder: Missing 'end' line."
|
||||
* </pre>
|
||||
*
|
||||
* @author Chuck McManis
|
||||
* @see CharacterDecoder
|
||||
* @see UUEncoder
|
||||
*/
|
||||
public class UUDecoder extends CharacterDecoder {
|
||||
|
||||
/**
|
||||
* This string contains the name that was in the buffer being decoded.
|
||||
*/
|
||||
public String bufferName;
|
||||
|
||||
/**
|
||||
* Represents UNIX(tm) mode bits. Generally three octal digits
|
||||
* representing read, write, and execute permission of the owner,
|
||||
* group owner, and others. They should be interpreted as the bit groups:
|
||||
* <pre>
|
||||
* (owner) (group) (others)
|
||||
* rwx rwx rwx (r = read, w = write, x = execute)
|
||||
*</pre>
|
||||
*
|
||||
*/
|
||||
public int mode;
|
||||
|
||||
|
||||
/**
|
||||
* UU encoding specifies 3 bytes per atom.
|
||||
*/
|
||||
protected int bytesPerAtom() {
|
||||
return (3);
|
||||
}
|
||||
|
||||
/**
|
||||
* All UU lines have 45 bytes on them, for line length of 15*4+1 or 61
|
||||
* characters per line.
|
||||
*/
|
||||
protected int bytesPerLine() {
|
||||
return (45);
|
||||
}
|
||||
|
||||
/** This is used to decode the atoms */
|
||||
private byte decoderBuffer[] = new byte[4];
|
||||
|
||||
/**
|
||||
* Decode a UU atom. Note that if l is less than 3 we don't write
|
||||
* the extra bits, however the encoder always encodes 4 character
|
||||
* groups even when they are not needed.
|
||||
*/
|
||||
protected void decodeAtom(PushbackInputStream inStream, OutputStream outStream, int l)
|
||||
throws IOException {
|
||||
int i, c1, c2, c3, c4;
|
||||
int a, b, c;
|
||||
StringBuilder x = new StringBuilder();
|
||||
|
||||
for (i = 0; i < 4; i++) {
|
||||
c1 = inStream.read();
|
||||
if (c1 == -1) {
|
||||
throw new CEStreamExhausted();
|
||||
}
|
||||
x.append((char)c1);
|
||||
decoderBuffer[i] = (byte) ((c1 - ' ') & 0x3f);
|
||||
}
|
||||
a = ((decoderBuffer[0] << 2) & 0xfc) | ((decoderBuffer[1] >>> 4) & 3);
|
||||
b = ((decoderBuffer[1] << 4) & 0xf0) | ((decoderBuffer[2] >>> 2) & 0xf);
|
||||
c = ((decoderBuffer[2] << 6) & 0xc0) | (decoderBuffer[3] & 0x3f);
|
||||
outStream.write((byte)(a & 0xff));
|
||||
if (l > 1) {
|
||||
outStream.write((byte)( b & 0xff));
|
||||
}
|
||||
if (l > 2) {
|
||||
outStream.write((byte)(c&0xff));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For uuencoded buffers, the data begins with a line of the form:
|
||||
* begin MODE FILENAME
|
||||
* This line always starts in column 1.
|
||||
*/
|
||||
protected void decodeBufferPrefix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
|
||||
int c;
|
||||
StringBuilder q = new StringBuilder(32);
|
||||
String r;
|
||||
boolean sawNewLine;
|
||||
|
||||
/*
|
||||
* This works by ripping through the buffer until it finds a 'begin'
|
||||
* line or the end of the buffer.
|
||||
*/
|
||||
sawNewLine = true;
|
||||
while (true) {
|
||||
c = inStream.read();
|
||||
if (c == -1) {
|
||||
throw new CEFormatException("UUDecoder: No begin line.");
|
||||
}
|
||||
if ((c == 'b') && sawNewLine){
|
||||
c = inStream.read();
|
||||
if (c == 'e') {
|
||||
break;
|
||||
}
|
||||
}
|
||||
sawNewLine = (c == '\n') || (c == '\r');
|
||||
}
|
||||
|
||||
/*
|
||||
* Now we think its begin, (we've seen ^be) so verify it here.
|
||||
*/
|
||||
while ((c != '\n') && (c != '\r')) {
|
||||
c = inStream.read();
|
||||
if (c == -1) {
|
||||
throw new CEFormatException("UUDecoder: No begin line.");
|
||||
}
|
||||
if ((c != '\n') && (c != '\r')) {
|
||||
q.append((char)c);
|
||||
}
|
||||
}
|
||||
r = q.toString();
|
||||
if (r.indexOf(' ') != 3) {
|
||||
throw new CEFormatException("UUDecoder: Malformed begin line.");
|
||||
}
|
||||
mode = Integer.parseInt(r.substring(4,7));
|
||||
bufferName = r.substring(r.indexOf(' ',6)+1);
|
||||
/*
|
||||
* Check for \n after \r
|
||||
*/
|
||||
if (c == '\r') {
|
||||
c = inStream.read ();
|
||||
if ((c != '\n') && (c != -1))
|
||||
inStream.unread (c);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* In uuencoded buffers, encoded lines start with a character that
|
||||
* represents the number of bytes encoded in this line. The last
|
||||
* line of input is always a line that starts with a single space
|
||||
* character, which would be a zero length line.
|
||||
*/
|
||||
protected int decodeLinePrefix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
|
||||
int c;
|
||||
|
||||
c = inStream.read();
|
||||
if (c == ' ') {
|
||||
c = inStream.read(); /* discard the (first)trailing CR or LF */
|
||||
c = inStream.read(); /* check for a second one */
|
||||
if ((c != '\n') && (c != -1))
|
||||
inStream.unread (c);
|
||||
throw new CEStreamExhausted();
|
||||
} else if (c == -1) {
|
||||
throw new CEFormatException("UUDecoder: Short Buffer.");
|
||||
}
|
||||
|
||||
c = (c - ' ') & 0x3f;
|
||||
if (c > bytesPerLine()) {
|
||||
throw new CEFormatException("UUDecoder: Bad Line Length.");
|
||||
}
|
||||
return (c);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Find the end of the line for the next operation.
|
||||
* The following sequences are recognized as end-of-line
|
||||
* CR, CR LF, or LF
|
||||
*/
|
||||
protected void decodeLineSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
|
||||
int c;
|
||||
while (true) {
|
||||
c = inStream.read();
|
||||
if (c == -1) {
|
||||
throw new CEStreamExhausted();
|
||||
}
|
||||
if (c == '\n') {
|
||||
break;
|
||||
}
|
||||
if (c == '\r') {
|
||||
c = inStream.read();
|
||||
if ((c != '\n') && (c != -1)) {
|
||||
inStream.unread (c);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* UUencoded files have a buffer suffix which consists of the word
|
||||
* end. This line should immediately follow the line with a single
|
||||
* space in it.
|
||||
*/
|
||||
protected void decodeBufferSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
|
||||
int c;
|
||||
|
||||
c = inStream.read(decoderBuffer);
|
||||
if ((decoderBuffer[0] != 'e') || (decoderBuffer[1] != 'n') ||
|
||||
(decoderBuffer[2] != 'd')) {
|
||||
throw new CEFormatException("UUDecoder: Missing 'end' line.");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,200 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 1995, 2004, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation. Oracle designates this
|
||||
* particular file as subject to the "Classpath" exception as provided
|
||||
* by Oracle in the LICENSE file that accompanied this code.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
package sun.misc;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* This class implements a Berkeley uu character encoder. This encoder
|
||||
* was made famous by uuencode program.
|
||||
*
|
||||
* The basic character coding is algorithmic, taking 6 bits of binary
|
||||
* data and adding it to an ASCII ' ' (space) character. This converts
|
||||
* these six bits into a printable representation. Note that it depends
|
||||
* on the ASCII character encoding standard for english. Groups of three
|
||||
* bytes are converted into 4 characters by treating the three bytes
|
||||
* a four 6 bit groups, group 1 is byte 1's most significant six bits,
|
||||
* group 2 is byte 1's least significant two bits plus byte 2's four
|
||||
* most significant bits. etc.
|
||||
*
|
||||
* In this encoding, the buffer prefix is:
|
||||
* <pre>
|
||||
* begin [mode] [filename]
|
||||
* </pre>
|
||||
*
|
||||
* This is followed by one or more lines of the form:
|
||||
* <pre>
|
||||
* (len)(data)(data)(data) ...
|
||||
* </pre>
|
||||
* where (len) is the number of bytes on this line. Note that groupings
|
||||
* are always four characters, even if length is not a multiple of three
|
||||
* bytes. When less than three characters are encoded, the values of the
|
||||
* last remaining bytes is undefined and should be ignored.
|
||||
*
|
||||
* The last line of data in a uuencoded file is represented by a single
|
||||
* space character. This is translated by the decoding engine to a line
|
||||
* length of zero. This is immediately followed by a line which contains
|
||||
* the word 'end[newline]'
|
||||
*
|
||||
* @author Chuck McManis
|
||||
* @see CharacterEncoder
|
||||
* @see UUDecoder
|
||||
*/
|
||||
public class UUEncoder extends CharacterEncoder {
|
||||
|
||||
/**
|
||||
* This name is stored in the begin line.
|
||||
*/
|
||||
private String bufferName;
|
||||
|
||||
/**
|
||||
* Represents UNIX(tm) mode bits. Generally three octal digits representing
|
||||
* read, write, and execute permission of the owner, group owner, and
|
||||
* others. They should be interpreted as the bit groups:
|
||||
* (owner) (group) (others)
|
||||
* rwx rwx rwx (r = read, w = write, x = execute)
|
||||
*
|
||||
* By default these are set to 644 (UNIX rw-r--r-- permissions).
|
||||
*/
|
||||
private int mode;
|
||||
|
||||
|
||||
/**
|
||||
* Default - buffer begin line will be:
|
||||
* <pre>
|
||||
* begin 644 encoder.buf
|
||||
* </pre>
|
||||
*/
|
||||
public UUEncoder() {
|
||||
bufferName = "encoder.buf";
|
||||
mode = 644;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies a name for the encoded buffer, begin line will be:
|
||||
* <pre>
|
||||
* begin 644 [FNAME]
|
||||
* </pre>
|
||||
*/
|
||||
public UUEncoder(String fname) {
|
||||
bufferName = fname;
|
||||
mode = 644;
|
||||
}
|
||||
|
||||
/**
|
||||
* Specifies a name and mode for the encoded buffer, begin line will be:
|
||||
* <pre>
|
||||
* begin [MODE] [FNAME]
|
||||
* </pre>
|
||||
*/
|
||||
public UUEncoder(String fname, int newMode) {
|
||||
bufferName = fname;
|
||||
mode = newMode;
|
||||
}
|
||||
|
||||
/** number of bytes per atom in uuencoding is 3 */
|
||||
protected int bytesPerAtom() {
|
||||
return (3);
|
||||
}
|
||||
|
||||
/** number of bytes per line in uuencoding is 45 */
|
||||
protected int bytesPerLine() {
|
||||
return (45);
|
||||
}
|
||||
|
||||
/**
|
||||
* encodeAtom - take three bytes and encodes them into 4 characters
|
||||
* If len is less than 3 then remaining bytes are filled with '1'.
|
||||
* This insures that the last line won't end in spaces and potentiallly
|
||||
* be truncated.
|
||||
*/
|
||||
protected void encodeAtom(OutputStream outStream, byte data[], int offset, int len)
|
||||
throws IOException {
|
||||
byte a, b = 1, c = 1;
|
||||
int c1, c2, c3, c4;
|
||||
|
||||
a = data[offset];
|
||||
if (len > 1) {
|
||||
b = data[offset+1];
|
||||
}
|
||||
if (len > 2) {
|
||||
c = data[offset+2];
|
||||
}
|
||||
|
||||
c1 = (a >>> 2) & 0x3f;
|
||||
c2 = ((a << 4) & 0x30) | ((b >>> 4) & 0xf);
|
||||
c3 = ((b << 2) & 0x3c) | ((c >>> 6) & 0x3);
|
||||
c4 = c & 0x3f;
|
||||
outStream.write(c1 + ' ');
|
||||
outStream.write(c2 + ' ');
|
||||
outStream.write(c3 + ' ');
|
||||
outStream.write(c4 + ' ');
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encode the line prefix which consists of the single character. The
|
||||
* lenght is added to the value of ' ' (32 decimal) and printed.
|
||||
*/
|
||||
protected void encodeLinePrefix(OutputStream outStream, int length)
|
||||
throws IOException {
|
||||
outStream.write((length & 0x3f) + ' ');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The line suffix for uuencoded files is simply a new line.
|
||||
*/
|
||||
protected void encodeLineSuffix(OutputStream outStream) throws IOException {
|
||||
pStream.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* encodeBufferPrefix writes the begin line to the output stream.
|
||||
*/
|
||||
protected void encodeBufferPrefix(OutputStream a) throws IOException {
|
||||
super.pStream = new PrintStream(a);
|
||||
super.pStream.print("begin "+mode+" ");
|
||||
if (bufferName != null) {
|
||||
super.pStream.println(bufferName);
|
||||
} else {
|
||||
super.pStream.println("encoder.bin");
|
||||
}
|
||||
super.pStream.flush();
|
||||
}
|
||||
|
||||
/**
|
||||
* encodeBufferSuffix writes the single line containing space (' ') and
|
||||
* the line containing the word 'end' to the output stream.
|
||||
*/
|
||||
protected void encodeBufferSuffix(OutputStream a) throws IOException {
|
||||
super.pStream.println(" \nend");
|
||||
super.pStream.flush();
|
||||
}
|
||||
|
||||
}
|
||||
@ -1,90 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2000, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4159554
|
||||
* @summary Problem with UUDecoder
|
||||
* @modules java.base/sun.misc
|
||||
*/
|
||||
|
||||
import sun.misc.*;
|
||||
import java.io.*;
|
||||
|
||||
public class DecodeBuffer {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
String encoded;
|
||||
// text to encode and decode
|
||||
String originalText = "Hi There, please encode and decode me";
|
||||
UUDecoder uuD = new UUDecoder();
|
||||
|
||||
encoded = "begin 644 encoder.buf\r\n" +
|
||||
"E2&D@5&AE<F4L('!L96%S92!E;F-O9&4@86YD(&1E8V]D92!M90$!\r\n"+
|
||||
" \r\nend\r\n";
|
||||
check (uuD, encoded, originalText);
|
||||
|
||||
encoded = "begin 644 encoder.buf\n" +
|
||||
"E2&D@5&AE<F4L('!L96%S92!E;F-O9&4@86YD(&1E8V]D92!M90$!\n"+
|
||||
" \nend\n";
|
||||
check (uuD, encoded, originalText);
|
||||
|
||||
encoded = "begin 644 encoder.buf\r" +
|
||||
"E2&D@5&AE<F4L('!L96%S92!E;F-O9&4@86YD(&1E8V]D92!M90$!\r"+
|
||||
" \rend\r";
|
||||
check (uuD, encoded, originalText);
|
||||
|
||||
// Multi-line Unix text file
|
||||
|
||||
String s1 = "begin 644 f\n"+
|
||||
"M3W)I9VYL(\"I(:2!4:&5R92P@<&QE87-E(&5N8V]D92!A;F0@9&5C;V1E(&UE\n"+
|
||||
"M*@IA;F0@;64@06YD($UE(&%N1\"!M92!!;F0@344@04Y$($U%(%I80U8@,3(S\n"+
|
||||
"-97)T\"E5)3U @45=%\"DUE\n"+
|
||||
" \nend\n";
|
||||
|
||||
String s2 = "Orignl *Hi There, please encode and decode me*\n"+
|
||||
"and me And Me anD me And ME AND ME ZXCV 123ert\n"+
|
||||
"UIOP QWE\n";
|
||||
check (uuD, s1, s2);
|
||||
|
||||
// Multi-line Windows text file
|
||||
|
||||
s1 = "begin 644 f\n"+
|
||||
"M2&5L;&\\@22!A;2!A(&UU;'1I;&EN92!F:6QE#0IC<F5A=&5D(&]N(%=I;F1O\r\n"+
|
||||
"M=W,L('1O('1E<W0@=&AE(%5516YC;V1E<@T*86YD(%551&5C;V1E<B!C;&%S\r\n"+
|
||||
"$<V5S+G1O\r\n"+ " \r\nend\r\n";
|
||||
s2="Hello I am a multiline file\r\n"+
|
||||
"created on Windows, to test the UUEncoder\r\n"+
|
||||
"and UUDecoder classes.";
|
||||
check (uuD, s1, s2);
|
||||
}
|
||||
|
||||
public static void check (UUDecoder uuD, String s, String original) throws Exception {
|
||||
String decoded;
|
||||
// do UU stuff
|
||||
decoded = new String(uuD.decodeBuffer(s));
|
||||
if (!decoded.equals (original)) {
|
||||
throw new Exception ("decoded text not same as original");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,55 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 4041231
|
||||
* @summary Test UUEncoder.java for proper masking in encodeAtom
|
||||
* @modules java.base/sun.misc
|
||||
*/
|
||||
|
||||
import sun.misc.*;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
public class Encode {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
UUEncoder encoder = new UUEncoder("encode.buf");
|
||||
byte[] buffer = new byte[3];
|
||||
|
||||
buffer[0] = -1;
|
||||
buffer[1] = -1;
|
||||
buffer[2] = -1;
|
||||
|
||||
ByteArrayInputStream in = new ByteArrayInputStream(buffer);
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream(10);
|
||||
|
||||
encoder.encodeBuffer(in, out);
|
||||
byte[] result = out.toByteArray();
|
||||
|
||||
if (result[22] == 31)
|
||||
throw new RuntimeException("UUEncoder generates incorrect byte sequences in encodeAtom.");
|
||||
|
||||
}
|
||||
}
|
||||
@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License version 2 only, as
|
||||
* published by the Free Software Foundation.
|
||||
*
|
||||
* This code is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 5031097
|
||||
* @summary sun.misc.CharacterEncoder(ByteBuffer) is dumping too
|
||||
* much information
|
||||
* @modules java.base/sun.misc
|
||||
* @author Brad Wetmore
|
||||
*/
|
||||
|
||||
import java.nio.*;
|
||||
import sun.misc.*;
|
||||
|
||||
public class GetBytes {
|
||||
|
||||
public static void main(String args[]) throws Exception {
|
||||
|
||||
ByteBuffer bb = ByteBuffer.wrap(new byte [26 + 2]);
|
||||
|
||||
for (int i = 'a'; i < 'a' + bb.capacity(); i++) {
|
||||
bb.put((byte)i);
|
||||
}
|
||||
|
||||
/*
|
||||
* Slice a subbuffer out of the original buffer.
|
||||
*/
|
||||
bb.position(1);
|
||||
bb.limit(bb.capacity() - 1);
|
||||
|
||||
ByteBuffer src = bb.slice();
|
||||
|
||||
CharacterEncoder e = new BASE64Encoder();
|
||||
CharacterDecoder d = new BASE64Decoder();
|
||||
|
||||
String encoded = e.encodeBuffer(src);
|
||||
ByteBuffer dst = d.decodeBufferToByteBuffer(encoded);
|
||||
|
||||
src.rewind();
|
||||
dst.rewind();
|
||||
|
||||
if (src.compareTo(dst) != 0) {
|
||||
throw new Exception("Didn't encode/decode correctly");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user