51 * 52 * In the CharacterEncoder and CharacterDecoder classes, one complete 53 * chunk of data is referred to as a <i>buffer</i>. Encoded buffers 54 * are all text, and decoded buffers (sometimes just referred to as 55 * buffers) are binary octets. 56 * 57 * To create a custom encoder, you must, at a minimum, overide three 58 * abstract methods in this class. 59 * <DL> 60 * <DD>bytesPerAtom which tells the encoder how many bytes to 61 * send to encodeAtom 62 * <DD>encodeAtom which encodes the bytes sent to it as text. 63 * <DD>bytesPerLine which tells the encoder the maximum number of 64 * bytes per line. 65 * </DL> 66 * 67 * Several useful encoders have already been written and are 68 * referenced in the See Also list below. 69 * 70 * @author Chuck McManis 71 * @see CharacterDecoder; 72 * @see UCEncoder 73 * @see UUEncoder 74 * @see BASE64Encoder 75 */ 76 public abstract class CharacterEncoder { 77 78 /** Stream that understands "printing" */ 79 protected PrintStream pStream; 80 81 /** Return the number of bytes per atom of encoding */ 82 abstract protected int bytesPerAtom(); 83 84 /** Return the number of bytes that can be encoded per line */ 85 abstract protected int bytesPerLine(); 86 87 /** 88 * Encode the prefix for the entire buffer. By default is simply 89 * opens the PrintStream for use by the other functions. 90 */ 91 protected void encodeBufferPrefix(OutputStream aStream) throws IOException { 92 pStream = new PrintStream(aStream); 93 } 94 95 /** 96 * Encode the suffix for the entire buffer. 97 */ 98 protected void encodeBufferSuffix(OutputStream aStream) throws IOException { 99 } 100 101 /** 102 * Encode the prefix that starts every output line. 103 */ 104 protected void encodeLinePrefix(OutputStream aStream, int aLength) 105 throws IOException { 106 } 107 108 /** 109 * Encode the suffix that ends every output line. By default 110 * this method just prints a <newline> into the output stream. 111 */ 112 protected void encodeLineSuffix(OutputStream aStream) throws IOException { 113 pStream.println(); 114 } 115 116 /** Encode one "atom" of information into characters. */ 117 abstract protected void encodeAtom(OutputStream aStream, byte someBytes[], 118 int anOffset, int aLength) throws IOException; 119 120 /** 121 * This method works around the bizarre semantics of BufferedInputStream's 122 * read method. 123 */ 124 protected int readFully(InputStream in, byte buffer[]) 125 throws java.io.IOException { 126 for (int i = 0; i < buffer.length; i++) { 127 int q = in.read(); 128 if (q == -1) 129 return i; 130 buffer[i] = (byte)q; | 51 * 52 * In the CharacterEncoder and CharacterDecoder classes, one complete 53 * chunk of data is referred to as a <i>buffer</i>. Encoded buffers 54 * are all text, and decoded buffers (sometimes just referred to as 55 * buffers) are binary octets. 56 * 57 * To create a custom encoder, you must, at a minimum, overide three 58 * abstract methods in this class. 59 * <DL> 60 * <DD>bytesPerAtom which tells the encoder how many bytes to 61 * send to encodeAtom 62 * <DD>encodeAtom which encodes the bytes sent to it as text. 63 * <DD>bytesPerLine which tells the encoder the maximum number of 64 * bytes per line. 65 * </DL> 66 * 67 * Several useful encoders have already been written and are 68 * referenced in the See Also list below. 69 * 70 * @author Chuck McManis 71 * @see CharacterDecoder 72 * @see UCEncoder 73 * @see UUEncoder 74 * @see BASE64Encoder 75 */ 76 public abstract class CharacterEncoder { 77 78 /** Stream that understands "printing" */ 79 protected PrintStream pStream; 80 81 /** Return the number of bytes per atom of encoding */ 82 abstract protected int bytesPerAtom(); 83 84 /** Return the number of bytes that can be encoded per line */ 85 abstract protected int bytesPerLine(); 86 87 /** 88 * Encode the prefix for the entire buffer. By default is simply 89 * opens the PrintStream for use by the other functions. 90 */ 91 protected void encodeBufferPrefix(OutputStream aStream) throws IOException { 92 pStream = new PrintStream(aStream); 93 } 94 95 /** 96 * Encode the suffix for the entire buffer. 97 */ 98 protected void encodeBufferSuffix(OutputStream aStream) throws IOException { 99 } 100 101 /** 102 * Encode the prefix that starts every output line. 103 */ 104 protected void encodeLinePrefix(OutputStream aStream, int aLength) 105 throws IOException { 106 } 107 108 /** 109 * Encode the suffix that ends every output line. By default 110 * this method just prints a newline into the output stream. 111 */ 112 protected void encodeLineSuffix(OutputStream aStream) throws IOException { 113 pStream.println(); 114 } 115 116 /** Encode one "atom" of information into characters. */ 117 abstract protected void encodeAtom(OutputStream aStream, byte someBytes[], 118 int anOffset, int aLength) throws IOException; 119 120 /** 121 * This method works around the bizarre semantics of BufferedInputStream's 122 * read method. 123 */ 124 protected int readFully(InputStream in, byte buffer[]) 125 throws java.io.IOException { 126 for (int i = 0; i < buffer.length; i++) { 127 int q = in.read(); 128 if (q == -1) 129 return i; 130 buffer[i] = (byte)q; |