/Users/lyon/j4p/src/security/CharacterEncoder.java

1    /* 
2     * @(#)CharacterEncoder.java    1.32 03/01/23 
3     * 
4     * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 
5     * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. 
6     */ 
7     
8    package security; 
9     
10   import java.io.InputStream; 
11   import java.io.ByteArrayInputStream; 
12   import java.io.OutputStream; 
13   import java.io.ByteArrayOutputStream; 
14   import java.io.PrintStream; 
15   import java.io.IOException; 
16    
17    
18   /** 
19    * This class defines the encoding half of character encoders. 
20    * A character encoder is an algorithim for transforming 8 bit binary 
21    * data into text (generally 7 bit ASCII or 8 bit ISO-Latin-1 text) 
22    * for transmition over text channels such as e-mail and network news. 
23    *  
24    * The character encoders have been structured around a central theme 
25    * that, in general, the encoded text has the form: 
26    * 
27    * <pre> 
28    *  [Buffer Prefix] 
29    *  [Line Prefix][encoded data atoms][Line Suffix] 
30    *  [Buffer Suffix] 
31    * </pre> 
32    * 
33    * In the CharacterEncoder and CharacterDecoder classes, one complete 
34    * chunk of data is referred to as a <i>buffer</i>. Encoded buffers  
35    * are all text, and decoded buffers (sometimes just referred to as  
36    * buffers) are binary octets. 
37    * 
38    * To create a custom encoder, you must, at a minimum,  overide three 
39    * abstract methods in this class. 
40    * <DL> 
41    * <DD>bytesPerAtom which tells the encoder how many bytes to  
42    * send to encodeAtom 
43    * <DD>encodeAtom which encodes the bytes sent to it as text. 
44    * <DD>bytesPerLine which tells the encoder the maximum number of 
45    * bytes per line. 
46    * </DL> 
47    * 
48    * Several useful encoders have already been written and are  
49    * referenced in the See Also list below. 
50    * 
51    * @version 1.32, 01/23/03 
52    * @author  Chuck McManis    
53    * @see     BASE64Encoder 
54    */ 
55   public abstract class CharacterEncoder { 
56    
57       /** Stream that understands "printing" */ 
58       protected PrintStream pStream; 
59    
60       /** Return the number of bytes per atom of encoding */ 
61       abstract protected int bytesPerAtom(); 
62    
63       /** Return the number of bytes that can be encoded per line */ 
64       abstract protected int bytesPerLine(); 
65    
66       /** 
67        * Encode the prefix for the entire buffer. By default is simply 
68        * opens the PrintStream for use by the other functions. 
69        */ 
70       protected void encodeBufferPrefix(OutputStream aStream) throws IOException { 
71       pStream = new PrintStream(aStream); 
72       } 
73    
74       /** 
75        * Encode the suffix for the entire buffer. 
76        */ 
77       protected void encodeBufferSuffix(OutputStream aStream) throws IOException { 
78       } 
79    
80       /** 
81        * Encode the prefix that starts every output line. 
82        */ 
83       protected void encodeLinePrefix(OutputStream aStream, int aLength) 
84       throws IOException { 
85       } 
86    
87       /** 
88        * Encode the suffix that ends every output line. By default 
89        * this method just prints a <newline> into the output stream. 
90        */ 
91       protected void encodeLineSuffix(OutputStream aStream) throws IOException { 
92       pStream.println(); 
93       } 
94    
95       /** Encode one "atom" of information into characters. */ 
96       abstract protected void encodeAtom(OutputStream aStream, byte someBytes[], 
97           int anOffset, int aLength) throws IOException; 
98    
99       /** 
100       * This method works around the bizarre semantics of BufferedInputStream's 
101       * read method. 
102       */ 
103      protected int readFully(InputStream in, byte buffer[])  
104      throws java.io.IOException { 
105      for (int i = 0; i < buffer.length; i++) { 
106          int q = in.read(); 
107          if (q == -1) 
108          return i; 
109          buffer[i] = (byte)q; 
110      } 
111      return buffer.length; 
112      } 
113   
114      /** 
115       * Encode bytes from the input stream, and write them as text characters 
116       * to the output stream. This method will run until it exhausts the 
117       * input stream, but does not print the line suffix for a final 
118       * line that is shorter than bytesPerLine(). 
119       */ 
120      public void encode(InputStream inStream, OutputStream outStream)  
121      throws IOException { 
122      int j; 
123      int numBytes; 
124      byte    tmpbuffer[] = new byte[bytesPerLine()]; 
125   
126      encodeBufferPrefix(outStream); 
127       
128      while (true) { 
129          numBytes = readFully(inStream, tmpbuffer); 
130          if (numBytes == 0) { 
131          break; 
132          } 
133          encodeLinePrefix(outStream, numBytes); 
134          for (j = 0; j < numBytes; j += bytesPerAtom()) { 
135   
136          if ((j + bytesPerAtom()) <= numBytes) { 
137              encodeAtom(outStream, tmpbuffer, j, bytesPerAtom()); 
138          } else { 
139              encodeAtom(outStream, tmpbuffer, j, (numBytes)- j); 
140          } 
141          } 
142          if (numBytes < bytesPerLine()) { 
143          break; 
144          } else { 
145          encodeLineSuffix(outStream); 
146          } 
147      } 
148      encodeBufferSuffix(outStream); 
149      } 
150   
151      /** 
152       * Encode the buffer in <i>aBuffer</i> and write the encoded 
153       * result to the OutputStream <i>aStream</i>. 
154       */ 
155      public void encode(byte aBuffer[], OutputStream aStream)  
156      throws IOException { 
157      ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer); 
158      encode(inStream, aStream); 
159      } 
160   
161      /** 
162       * A 'streamless' version of encode that simply takes a buffer of 
163       * bytes and returns a string containing the encoded buffer. 
164       */ 
165      public String encode(byte aBuffer[]) { 
166      ByteArrayOutputStream   outStream = new ByteArrayOutputStream(); 
167      ByteArrayInputStream    inStream = new ByteArrayInputStream(aBuffer); 
168      String retVal = null; 
169      try { 
170          encode(inStream, outStream); 
171          // explicit ascii->unicode conversion 
172          retVal = outStream.toString("8859_1"); 
173      } catch (Exception IOException) { 
174          // This should never happen. 
175          throw new Error("ChracterEncoder::encodeBuffer internal error"); 
176      } 
177      return (retVal); 
178      } 
179   
180      /** 
181       * Encode bytes from the input stream, and write them as text characters 
182       * to the output stream. This method will run until it exhausts the 
183       * input stream. It differs from encode in that it will add the 
184       * line at the end of a final line that is shorter than bytesPerLine(). 
185       */ 
186      public void encodeBuffer(InputStream inStream, OutputStream outStream)  
187      throws IOException { 
188      int j; 
189      int numBytes; 
190      byte    tmpbuffer[] = new byte[bytesPerLine()]; 
191   
192      encodeBufferPrefix(outStream); 
193       
194      while (true) { 
195          numBytes = readFully(inStream, tmpbuffer); 
196          if (numBytes == 0) { 
197          break; 
198          } 
199          encodeLinePrefix(outStream, numBytes); 
200          for (j = 0; j < numBytes; j += bytesPerAtom()) { 
201          if ((j + bytesPerAtom()) <= numBytes) { 
202              encodeAtom(outStream, tmpbuffer, j, bytesPerAtom()); 
203          } else { 
204              encodeAtom(outStream, tmpbuffer, j, (numBytes)- j); 
205          } 
206          } 
207          encodeLineSuffix(outStream); 
208          if (numBytes < bytesPerLine()) { 
209          break;   
210          } 
211      } 
212      encodeBufferSuffix(outStream); 
213      } 
214   
215      /** 
216       * Encode the buffer in <i>aBuffer</i> and write the encoded 
217       * result to the OutputStream <i>aStream</i>. 
218       */ 
219      public void encodeBuffer(byte aBuffer[], OutputStream aStream)  
220      throws IOException { 
221      ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer); 
222      encodeBuffer(inStream, aStream); 
223      } 
224   
225      /** 
226       * A 'streamless' version of encode that simply takes a buffer of 
227       * bytes and returns a string containing the encoded buffer. 
228       */ 
229      public String encodeBuffer(byte aBuffer[]) { 
230      ByteArrayOutputStream   outStream = new ByteArrayOutputStream(); 
231      ByteArrayInputStream    inStream = new ByteArrayInputStream(aBuffer); 
232      try { 
233          encodeBuffer(inStream, outStream); 
234      } catch (Exception IOException) { 
235          // This should never happen. 
236          throw new Error("ChracterEncoder::encodeBuffer internal error"); 
237      } 
238      return (outStream.toString()); 
239      } 
240   
241  } 
242