Source Code Cross Referenced for CharacterDecoder.java in  » 6.0-JDK-Modules » j2me » sun » misc » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Modules » j2me » sun.misc 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * @(#)CharacterDecoder.java	1.23 06/10/10
003:         *
004:         * Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.  
005:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER  
006:         *   
007:         * This program is free software; you can redistribute it and/or  
008:         * modify it under the terms of the GNU General Public License version  
009:         * 2 only, as published by the Free Software Foundation.   
010:         *   
011:         * This program is distributed in the hope that it will be useful, but  
012:         * WITHOUT ANY WARRANTY; without even the implied warranty of  
013:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU  
014:         * General Public License version 2 for more details (a copy is  
015:         * included at /legal/license.txt).   
016:         *   
017:         * You should have received a copy of the GNU General Public License  
018:         * version 2 along with this work; if not, write to the Free Software  
019:         * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  
020:         * 02110-1301 USA   
021:         *   
022:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa  
023:         * Clara, CA 95054 or visit www.sun.com if you need additional  
024:         * information or have any questions. 
025:         *
026:         */
027:
028:        package sun.misc;
029:
030:        import java.io.OutputStream;
031:        import java.io.ByteArrayOutputStream;
032:        import java.io.InputStream;
033:        import java.io.ByteArrayInputStream;
034:        import java.io.IOException;
035:
036:        /**
037:         * This class defines the decoding half of character encoders.
038:         * A character decoder is an algorithim for transforming 8 bit
039:         * binary data that has been encoded into text by a character
040:         * encoder, back into original binary form.
041:         * 
042:         * The character encoders, in general, have been structured 
043:         * around a central theme that binary data can be encoded into
044:         * text that has the form:
045:         *
046:         * <pre>
047:         *	[Buffer Prefix]
048:         *	[Line Prefix][encoded data atoms][Line Suffix]
049:         *	[Buffer Suffix]
050:         * </pre>
051:         *
052:         * Of course in the simplest encoding schemes, the buffer has no
053:         * distinct prefix of suffix, however all have some fixed relationship
054:         * between the text in an 'atom' and the binary data itself.
055:         *
056:         * In the CharacterEncoder and CharacterDecoder classes, one complete
057:         * chunk of data is referred to as a <i>buffer</i>. Encoded buffers 
058:         * are all text, and decoded buffers (sometimes just referred to as 
059:         * buffers) are binary octets.
060:         *
061:         * To create a custom decoder, you must, at a minimum,  overide three
062:         * abstract methods in this class.
063:         * <DL>
064:         * <DD>bytesPerAtom which tells the decoder how many bytes to 
065:         * expect from decodeAtom
066:         * <DD>decodeAtom which decodes the bytes sent to it as text.
067:         * <DD>bytesPerLine which tells the encoder the maximum number of
068:         * bytes per line.
069:         * </DL>
070:         *
071:         * In general, the character decoders return error in the form of a
072:         * CEFormatException. The syntax of the detail string is
073:         * <pre>
074:         *	DecoderClassName: Error message.
075:         * </pre>
076:         *
077:         * Several useful decoders have already been written and are 
078:         * referenced in the See Also list below.
079:         *
080:         * @version	05/03/00, 1.17
081:         * @author	Chuck McManis
082:         * @see		CEFormatException
083:         * @see		CharacterEncoder
084:         * @see		UCDecoder
085:         * @see		UUDecoder
086:         * @see		BASE64Decoder
087:         */
088:
089:        public abstract class CharacterDecoder {
090:
091:            /** Return the number of bytes per atom of decoding */
092:            abstract protected int bytesPerAtom();
093:
094:            /** Return the maximum number of bytes that can be encoded per line */
095:            abstract protected int bytesPerLine();
096:
097:            /** decode the beginning of the buffer, by default this is a NOP. */
098:            protected void decodeBufferPrefix(InputStream aStream,
099:                    OutputStream bStream) throws IOException {
100:            }
101:
102:            /** decode the buffer suffix, again by default it is a NOP. */
103:            protected void decodeBufferSuffix(InputStream aStream,
104:                    OutputStream bStream) throws IOException {
105:            }
106:
107:            /**
108:             * This method should return, if it knows, the number of bytes
109:             * that will be decoded. Many formats such as uuencoding provide
110:             * this information. By default we return the maximum bytes that
111:             * could have been encoded on the line.
112:             */
113:            protected int decodeLinePrefix(InputStream aStream,
114:                    OutputStream bStream) throws IOException {
115:                return (bytesPerLine());
116:            }
117:
118:            /**
119:             * This method post processes the line, if there are error detection
120:             * or correction codes in a line, they are generally processed by
121:             * this method. The simplest version of this method looks for the
122:             * (newline) character. 
123:             */
124:            protected void decodeLineSuffix(InputStream aStream,
125:                    OutputStream bStream) throws IOException {
126:            }
127:
128:            /**
129:             * This method does an actual decode. It takes the decoded bytes and
130:             * writes them to the OuputStream. The integer <i>l</i> tells the
131:             * method how many bytes are required. This is always <= bytesPerAtom().
132:             */
133:            protected void decodeAtom(InputStream aStream,
134:                    OutputStream bStream, int l) throws IOException {
135:                throw new CEStreamExhausted();
136:            }
137:
138:            /**
139:             * This method works around the bizarre semantics of BufferedInputStream's
140:             * read method.
141:             */
142:            protected int readFully(InputStream in, byte buffer[], int offset,
143:                    int len) throws java.io.IOException {
144:                for (int i = 0; i < len; i++) {
145:                    int q = in.read();
146:                    if (q == -1)
147:                        return ((i == 0) ? -1 : i);
148:                    buffer[i + offset] = (byte) q;
149:                }
150:                return len;
151:            }
152:
153:            /**
154:             * Decode the text from the InputStream and write the decoded
155:             * octets to the OutputStream. This method runs until the stream
156:             * is exhausted.
157:             * @exception CEFormatException An error has occured while decoding
158:             * @exception CEStreamExhausted The input stream is unexpectedly out of data
159:             */
160:            public void decodeBuffer(InputStream aStream, OutputStream bStream)
161:                    throws IOException {
162:                int i;
163:                int totalBytes = 0;
164:
165:                decodeBufferPrefix(aStream, bStream);
166:                while (true) {
167:                    int length;
168:
169:                    try {
170:                        length = decodeLinePrefix(aStream, bStream);
171:                        for (i = 0; (i + bytesPerAtom()) < length; i += bytesPerAtom()) {
172:                            decodeAtom(aStream, bStream, bytesPerAtom());
173:                            totalBytes += bytesPerAtom();
174:                        }
175:                        if ((i + bytesPerAtom()) == length) {
176:                            decodeAtom(aStream, bStream, bytesPerAtom());
177:                            totalBytes += bytesPerAtom();
178:                        } else {
179:                            decodeAtom(aStream, bStream, length - i);
180:                            totalBytes += (length - i);
181:                        }
182:                        decodeLineSuffix(aStream, bStream);
183:                    } catch (CEStreamExhausted e) {
184:                        break;
185:                    }
186:                }
187:                decodeBufferSuffix(aStream, bStream);
188:            }
189:
190:            /**
191:             * Alternate decode interface that takes a String containing the encoded
192:             * buffer and returns a byte array containing the data.
193:             * @exception CEFormatException An error has occured while decoding
194:             */
195:            public byte decodeBuffer(String inputString)[] throws IOException {
196:                byte inputBuffer[] = new byte[inputString.length()];
197:                ByteArrayInputStream inStream;
198:                ByteArrayOutputStream outStream;
199:
200:                inputBuffer = inputString.getBytes();
201:                inStream = new ByteArrayInputStream(inputBuffer);
202:                outStream = new ByteArrayOutputStream();
203:                decodeBuffer(inStream, outStream);
204:                return (outStream.toByteArray());
205:            }
206:
207:            /**
208:             * Decode the contents of the inputstream into a buffer.
209:             */
210:            public byte decodeBuffer(InputStream in)[] throws IOException {
211:                ByteArrayOutputStream outStream = new ByteArrayOutputStream();
212:                decodeBuffer(in, outStream);
213:                return (outStream.toByteArray());
214:            }
215:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.