Source Code Cross Referenced for StringRepresentation.java in  » Web-Services » restlet-1.0.8 » org » restlet » resource » 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 » Web Services » restlet 1.0.8 » org.restlet.resource 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2005-2007 Noelios Consulting.
003:         *
004:         * The contents of this file are subject to the terms of the Common Development
005:         * and Distribution License (the "License"). You may not use this file except in
006:         * compliance with the License.
007:         *
008:         * You can obtain a copy of the license at
009:         * http://www.opensource.org/licenses/cddl1.txt See the License for the specific
010:         * language governing permissions and limitations under the License.
011:         *
012:         * When distributing Covered Code, include this CDDL HEADER in each file and
013:         * include the License file at http://www.opensource.org/licenses/cddl1.txt If
014:         * applicable, add the following below this CDDL HEADER, with the fields
015:         * enclosed by brackets "[]" replaced with your own identifying information:
016:         * Portions Copyright [yyyy] [name of copyright owner]
017:         */
018:
019:        package org.restlet.resource;
020:
021:        import java.io.ByteArrayInputStream;
022:        import java.io.IOException;
023:        import java.io.InputStream;
024:        import java.io.OutputStream;
025:        import java.io.OutputStreamWriter;
026:        import java.io.UnsupportedEncodingException;
027:        import java.util.logging.Logger;
028:
029:        import org.restlet.data.CharacterSet;
030:        import org.restlet.data.Language;
031:        import org.restlet.data.MediaType;
032:
033:        /**
034:         * Represents an Unicode string that can be converted to any character set
035:         * supported by Java.
036:         *
037:         * @author Jerome Louvel (contact@noelios.com)
038:         */
039:        public class StringRepresentation extends StreamRepresentation {
040:            private CharSequence text;
041:
042:            /**
043:             * Constructor. The following metadata are used by default: "text/plain"
044:             * media type, no language and the ISO-8859-1 character set.
045:             *
046:             * @param text
047:             *            The string value.
048:             */
049:            public StringRepresentation(CharSequence text) {
050:                this (text, MediaType.TEXT_PLAIN);
051:            }
052:
053:            /**
054:             * Constructor. The following metadata are used by default: "text/plain"
055:             * media type, no language and the ISO-8859-1 character set.
056:             *
057:             * @param text
058:             *            The string value.
059:             * @param language
060:             *            The language.
061:             */
062:            public StringRepresentation(CharSequence text, Language language) {
063:                this (text, MediaType.TEXT_PLAIN, language);
064:            }
065:
066:            /**
067:             * Constructor. The following metadata are used by default: no language and
068:             * the ISO-8859-1 character set.
069:             *
070:             * @param text
071:             *            The string value.
072:             * @param mediaType
073:             *            The media type.
074:             */
075:            public StringRepresentation(CharSequence text, MediaType mediaType) {
076:                this (text, mediaType, null);
077:            }
078:
079:            /**
080:             * Constructor. The following metadata are used by default: ISO-8859-1
081:             * character set.
082:             *
083:             * @param text
084:             *            The string value.
085:             * @param mediaType
086:             *            The media type.
087:             * @param language
088:             *            The language.
089:             */
090:            public StringRepresentation(CharSequence text, MediaType mediaType,
091:                    Language language) {
092:                this (text, mediaType, language, CharacterSet.ISO_8859_1);
093:            }
094:
095:            /**
096:             * Constructor.
097:             *
098:             * @param text
099:             *            The string value.
100:             * @param mediaType
101:             *            The media type.
102:             * @param language
103:             *            The language.
104:             * @param characterSet
105:             *            The character set.
106:             */
107:            public StringRepresentation(CharSequence text, MediaType mediaType,
108:                    Language language, CharacterSet characterSet) {
109:                super (mediaType);
110:                this .text = text;
111:                setMediaType(mediaType);
112:                if (language != null) {
113:                    getLanguages().add(language);
114:                }
115:
116:                setCharacterSet(characterSet);
117:                updateSize();
118:            }
119:
120:            /**
121:             * Returns a stream with the representation's content. This method is
122:             * ensured to return a fresh stream for each invocation unless it is a
123:             * transient representation, in which case null is returned.
124:             *
125:             * @return A stream with the representation's content.
126:             * @throws IOException
127:             */
128:            public InputStream getStream() throws IOException {
129:                if (getText() != null) {
130:                    if (getCharacterSet() != null) {
131:                        return new ByteArrayInputStream(getText().getBytes(
132:                                getCharacterSet().getName()));
133:                    } else {
134:                        return new ByteArrayInputStream(getText().getBytes());
135:                    }
136:                } else {
137:                    return null;
138:                }
139:            }
140:
141:            /**
142:             * Converts the representation to a string value. Be careful when using this
143:             * method as the conversion of large content to a string fully stored in
144:             * memory can result in OutOfMemoryErrors being thrown.
145:             *
146:             * @return The representation as a string value.
147:             */
148:            public String getText() {
149:                return (this .text == null) ? null : this .text.toString();
150:            }
151:
152:            /**
153:             * Sets the string value.
154:             *
155:             * @param text
156:             *            The string value.
157:             */
158:            public void setText(String text) {
159:                this .text = text;
160:                updateSize();
161:            }
162:
163:            /**
164:             * Sets the character set or null if not applicable.
165:             *
166:             * @param characterSet
167:             *            The character set or null if not applicable.
168:             */
169:            public void setCharacterSet(CharacterSet characterSet) {
170:                super .setCharacterSet(characterSet);
171:                updateSize();
172:            }
173:
174:            /**
175:             * Updates the expected size according to the current string value.
176:             */
177:            protected void updateSize() {
178:                if (getText() != null) {
179:                    try {
180:                        if (getCharacterSet() != null) {
181:                            setSize(getText().getBytes(
182:                                    getCharacterSet().getName()).length);
183:                        } else {
184:                            setSize(getText().getBytes().length);
185:                        }
186:                    } catch (UnsupportedEncodingException e) {
187:                        Logger.getLogger(StringRepresentation.class
188:                                .getCanonicalName());
189:                        setSize(UNKNOWN_SIZE);
190:                    }
191:                } else {
192:                    setSize(UNKNOWN_SIZE);
193:                }
194:            }
195:
196:            /**
197:             * Writes the representation to a byte stream. This method is ensured to
198:             * write the full content for each invocation unless it is a transient
199:             * representation, in which case an exception is thrown.
200:             *
201:             * @param outputStream
202:             *            The output stream.
203:             * @throws IOException
204:             */
205:            public void write(OutputStream outputStream) throws IOException {
206:                if (getText() != null) {
207:                    OutputStreamWriter osw = null;
208:
209:                    if (getCharacterSet() != null) {
210:                        osw = new OutputStreamWriter(outputStream,
211:                                getCharacterSet().getName());
212:                    } else {
213:                        osw = new OutputStreamWriter(outputStream);
214:                    }
215:
216:                    osw.write(getText());
217:                    osw.flush();
218:                }
219:            }
220:
221:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.