Source Code Cross Referenced for DERPrintableString.java in  » Security » Bouncy-Castle » org » bouncycastle » asn1 » 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 » Security » Bouncy Castle » org.bouncycastle.asn1 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.bouncycastle.asn1;
002:
003:        import java.io.IOException;
004:
005:        /**
006:         * DER PrintableString object.
007:         */
008:        public class DERPrintableString extends ASN1Object implements  DERString {
009:            String string;
010:
011:            /**
012:             * return a printable string from the passed in object.
013:             * 
014:             * @exception IllegalArgumentException if the object cannot be converted.
015:             */
016:            public static DERPrintableString getInstance(Object obj) {
017:                if (obj == null || obj instanceof  DERPrintableString) {
018:                    return (DERPrintableString) obj;
019:                }
020:
021:                if (obj instanceof  ASN1OctetString) {
022:                    return new DERPrintableString(((ASN1OctetString) obj)
023:                            .getOctets());
024:                }
025:
026:                if (obj instanceof  ASN1TaggedObject) {
027:                    return getInstance(((ASN1TaggedObject) obj).getObject());
028:                }
029:
030:                throw new IllegalArgumentException(
031:                        "illegal object in getInstance: "
032:                                + obj.getClass().getName());
033:            }
034:
035:            /**
036:             * return a Printable String from a tagged object.
037:             *
038:             * @param obj the tagged object holding the object we want
039:             * @param explicit true if the object is meant to be explicitly
040:             *              tagged false otherwise.
041:             * @exception IllegalArgumentException if the tagged object cannot
042:             *               be converted.
043:             */
044:            public static DERPrintableString getInstance(ASN1TaggedObject obj,
045:                    boolean explicit) {
046:                return getInstance(obj.getObject());
047:            }
048:
049:            /**
050:             * basic constructor - byte encoded string.
051:             */
052:            public DERPrintableString(byte[] string) {
053:                char[] cs = new char[string.length];
054:
055:                for (int i = 0; i != cs.length; i++) {
056:                    cs[i] = (char) (string[i] & 0xff);
057:                }
058:
059:                this .string = new String(cs);
060:            }
061:
062:            /**
063:             * basic constructor - this does not validate the string
064:             */
065:            public DERPrintableString(String string) {
066:                this (string, false);
067:            }
068:
069:            /**
070:             * Constructor with optional validation.
071:             *
072:             * @param string the base string to wrap.
073:             * @param validate whether or not to check the string.
074:             * @throws IllegalArgumentException if validate is true and the string
075:             * contains characters that should not be in a PrintableString.
076:             */
077:            public DERPrintableString(String string, boolean validate) {
078:                if (validate && !isPrintableString(string)) {
079:                    throw new IllegalArgumentException(
080:                            "string contains illegal characters");
081:                }
082:
083:                this .string = string;
084:            }
085:
086:            public String getString() {
087:                return string;
088:            }
089:
090:            public byte[] getOctets() {
091:                char[] cs = string.toCharArray();
092:                byte[] bs = new byte[cs.length];
093:
094:                for (int i = 0; i != cs.length; i++) {
095:                    bs[i] = (byte) cs[i];
096:                }
097:
098:                return bs;
099:            }
100:
101:            void encode(DEROutputStream out) throws IOException {
102:                out.writeEncoded(PRINTABLE_STRING, this .getOctets());
103:            }
104:
105:            public int hashCode() {
106:                return this .getString().hashCode();
107:            }
108:
109:            boolean asn1Equals(DERObject o) {
110:                if (!(o instanceof  DERPrintableString)) {
111:                    return false;
112:                }
113:
114:                DERPrintableString s = (DERPrintableString) o;
115:
116:                return this .getString().equals(s.getString());
117:            }
118:
119:            public String toString() {
120:                return string;
121:            }
122:
123:            /**
124:             * return true if the passed in String can be represented without
125:             * loss as a PrintableString, false otherwise.
126:             *
127:             * @return true if in printable set, false otherwise.
128:             */
129:            public static boolean isPrintableString(String str) {
130:                for (int i = str.length() - 1; i >= 0; i--) {
131:                    char ch = str.charAt(i);
132:
133:                    if (ch > 0x007f) {
134:                        return false;
135:                    }
136:
137:                    if ('a' <= ch && ch <= 'z') {
138:                        continue;
139:                    }
140:
141:                    if ('A' <= ch && ch <= 'Z') {
142:                        continue;
143:                    }
144:
145:                    if ('0' <= ch && ch <= '9') {
146:                        continue;
147:                    }
148:
149:                    switch (ch) {
150:                    case ' ':
151:                    case '\'':
152:                    case '(':
153:                    case ')':
154:                    case '+':
155:                    case '-':
156:                    case '.':
157:                    case ':':
158:                    case '=':
159:                    case '?':
160:                    case '/':
161:                    case ',':
162:                        continue;
163:                    }
164:
165:                    return false;
166:                }
167:
168:                return true;
169:            }
170:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.