Source Code Cross Referenced for ICUBinary.java in  » Internationalization-Localization » icu4j » com » ibm » icu » impl » 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 » Internationalization Localization » icu4j » com.ibm.icu.impl 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         *******************************************************************************
003:         * Copyright (C) 1996-2004, International Business Machines Corporation and    *
004:         * others. All Rights Reserved.                                                *
005:         *******************************************************************************
006:         */
007:        package com.ibm.icu.impl;
008:
009:        import java.io.InputStream;
010:        import java.io.DataInputStream;
011:        import java.io.IOException;
012:        import java.util.Arrays;
013:
014:        public final class ICUBinary {
015:            // public inner interface ------------------------------------------------
016:
017:            /**
018:             * Special interface for data authentication
019:             */
020:            public static interface Authenticate {
021:                /**
022:                 * Method used in ICUBinary.readHeader() to provide data format
023:                 * authentication. 
024:                 * @param version version of the current data
025:                 * @return true if dataformat is an acceptable version, false otherwise
026:                 */
027:                public boolean isDataVersionAcceptable(byte version[]);
028:            }
029:
030:            // public methods --------------------------------------------------------
031:
032:            /**
033:             * <p>ICU data header reader method. 
034:             * Takes a ICU generated big-endian input stream, parse the ICU standard 
035:             * file header and authenticates them.</p>
036:             * <p>Header format: 
037:             * <ul>
038:             *     <li> Header size (char)
039:             *     <li> Magic number 1 (byte)
040:             *     <li> Magic number 2 (byte)
041:             *     <li> Rest of the header size (char)
042:             *     <li> Reserved word (char)
043:             *     <li> Big endian indicator (byte)
044:             *     <li> Character set family indicator (byte)
045:             *     <li> Size of a char (byte) for c++ and c use
046:             *     <li> Reserved byte (byte)
047:             *     <li> Data format identifier (4 bytes), each ICU data has its own
048:             *          identifier to distinguish them. [0] major [1] minor 
049:             *                                          [2] milli [3] micro 
050:             *     <li> Data version (4 bytes), the change version of the ICU data
051:             *                             [0] major [1] minor [2] milli [3] micro 
052:             *     <li> Unicode version (4 bytes) this ICU is based on.
053:             * </ul>
054:             * </p>
055:             * <p>
056:             * Example of use:<br>
057:             * <pre>
058:             * try {
059:             *    FileInputStream input = new FileInputStream(filename);
060:             *    If (Utility.readICUDataHeader(input, dataformat, dataversion, 
061:             *                                  unicode) {
062:             *        System.out.println("Verified file header, this is a ICU data file");
063:             *    }
064:             * } catch (IOException e) {
065:             *    System.out.println("This is not a ICU data file");
066:             * }
067:             * </pre>
068:             * </p>
069:             * @param inputStream input stream that contains the ICU data header
070:             * @param dataFormatIDExpected Data format expected. An array of 4 bytes 
071:             *                     information about the data format.
072:             *                     E.g. data format ID 1.2.3.4. will became an array of 
073:             *                     {1, 2, 3, 4}
074:             * @param authenticate user defined extra data authentication. This value
075:             *                     can be null, if no extra authentication is needed.
076:             * @exception IOException thrown if there is a read error or 
077:             *            when header authentication fails.
078:             * @draft 2.1
079:             */
080:            public static final byte[] readHeader(InputStream inputStream,
081:                    byte dataFormatIDExpected[], Authenticate authenticate)
082:                    throws IOException {
083:                DataInputStream input = new DataInputStream(inputStream);
084:                char headersize = input.readChar();
085:                int readcount = 2;
086:                //reading the header format
087:                byte magic1 = input.readByte();
088:                readcount++;
089:                byte magic2 = input.readByte();
090:                readcount++;
091:                if (magic1 != MAGIC1 || magic2 != MAGIC2) {
092:                    throw new IOException(MAGIC_NUMBER_AUTHENTICATION_FAILED_);
093:                }
094:
095:                input.readChar(); // reading size
096:                readcount += 2;
097:                input.readChar(); // reading reserved word
098:                readcount += 2;
099:                byte bigendian = input.readByte();
100:                readcount++;
101:                byte charset = input.readByte();
102:                readcount++;
103:                byte charsize = input.readByte();
104:                readcount++;
105:                input.readByte(); // reading reserved byte
106:                readcount++;
107:
108:                byte dataFormatID[] = new byte[4];
109:                input.readFully(dataFormatID);
110:                readcount += 4;
111:                byte dataVersion[] = new byte[4];
112:                input.readFully(dataVersion);
113:                readcount += 4;
114:                byte unicodeVersion[] = new byte[4];
115:                input.readFully(unicodeVersion);
116:                readcount += 4;
117:                if (headersize < readcount) {
118:                    throw new IOException("Internal Error: Header size error");
119:                }
120:                input.skipBytes(headersize - readcount);
121:
122:                if (bigendian != BIG_ENDIAN_
123:                        || charset != CHAR_SET_
124:                        || charsize != CHAR_SIZE_
125:                        || !Arrays.equals(dataFormatIDExpected, dataFormatID)
126:                        || (authenticate != null && !authenticate
127:                                .isDataVersionAcceptable(dataVersion))) {
128:                    throw new IOException(HEADER_AUTHENTICATION_FAILED_);
129:                }
130:                return unicodeVersion;
131:            }
132:
133:            // private variables -------------------------------------------------
134:
135:            /**
136:             * Magic numbers to authenticate the data file
137:             */
138:            private static final byte MAGIC1 = (byte) 0xda;
139:            private static final byte MAGIC2 = (byte) 0x27;
140:
141:            /**
142:             * File format authentication values
143:             */
144:            private static final byte BIG_ENDIAN_ = 1;
145:            private static final byte CHAR_SET_ = 0;
146:            private static final byte CHAR_SIZE_ = 2;
147:
148:            /**
149:             * Error messages
150:             */
151:            private static final String MAGIC_NUMBER_AUTHENTICATION_FAILED_ = "ICU data file error: Not an ICU data file";
152:            private static final String HEADER_AUTHENTICATION_FAILED_ = "ICU data file error: Header authentication failed, please check if you have a valid ICU data file";
153:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.