001: /*
002: *
003: *
004: * Copyright 1990-2007 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: package com.sun.j2me.global;
027:
028: import java.io.ByteArrayInputStream;
029: import java.io.ByteArrayOutputStream;
030: import java.io.DataInputStream;
031: import java.io.DataOutputStream;
032: import java.io.IOException;
033: import javax.microedition.global.ResourceException;
034:
035: /**
036: * <code>DateFormatSymbols</code> is class for encapsulating localizable
037: * date/time formatting data, such as the names of the months, the names of the
038: * days of the week, and the time zone data. <code>DateTimeFormat</code> uses
039: * <code>DateFormatSymbols</code> to encapsulate this information. <p>
040: *
041: * DateFormatSymbols is typicaly obtained from <code>
042: * javax.microedition.ResourceManager</code>
043: * respectively from <code>DevResourceManager.getDateFormatSymbols()</code>
044: * from resource file for given locale. <p>
045: *
046: * Typically you shouldn't use <code>DateFormatSymbols</code> directly. Rather,
047: * you are encouraged to create a date/time formatter with the
048: * <code>DateTimeFormat</code> class's factory methods:
049: * <code>getInstance(int style, String locale)</code>
050: * These methods automatically create a <code>DateFormatSymbols</code> for the
051: * formatter so that you don't have to. All fields are public intentionaly.
052: *
053: */
054: public class DateFormatSymbols implements SerializableResource {
055:
056: /**
057: * Create new DateFormatSymbol uninitialized.
058: */
059: public DateFormatSymbols() {
060: }
061:
062: /**
063: * The method clones the resource.
064: *
065: * @return copy of the resource or <code>null<code>
066: * if clonning wasn't possible
067: */
068: public java.lang.Object clone() {
069: DateFormatSymbols newDfs = null;
070: try {
071: ByteArrayOutputStream baos = new ByteArrayOutputStream();
072: write(baos);
073: baos.close();
074: byte[] buffer = baos.toByteArray();
075: ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
076: newDfs = new DateFormatSymbols();
077: newDfs.read(bais);
078: } catch (IOException ioe) {
079: // cannot clone resource
080: }
081: return newDfs;
082: }
083:
084: /**
085: * eras.
086: */
087: public String[] eras = new String[2];
088:
089: /**
090: * month names.
091: */
092: public String[] months = new String[13];
093:
094: /**
095: * short month names.
096: */
097: public String[] shortMonths = new String[13];
098:
099: /**
100: * day in week names.
101: */
102: public String[] weekDays = new String[7];
103:
104: /**
105: * short day in week names.
106: */
107: public String[] shortWeekDays = new String[7];
108:
109: /**
110: * ampms.
111: */
112: public String[] ampms = new String[2];
113:
114: /**
115: * localized patterns 6 possible styles as they are defined in {@link
116: * DateTimeFormat}.
117: */
118: public String[] patterns = new String[6];
119:
120: /**
121: * locale of this symbols.
122: */
123: public String locale = new String();
124:
125: /**
126: * Read DateFormatSymbols object from input stream.
127: *
128: * @param in input stream
129: * @throws java.io.IOException error reading resource
130: * @throws javax.microedition.global.ResourceException error creating
131: * resource
132: */
133: public void read(java.io.InputStream in) throws IOException,
134: ResourceException {
135: DataInputStream dis = new DataInputStream(in);
136: readStrings(eras, dis);
137: readStrings(months, dis);
138: readStrings(shortMonths, dis);
139: readStrings(weekDays, dis);
140: readStrings(shortWeekDays, dis);
141: readStrings(ampms, dis);
142: readStrings(patterns, dis);
143: locale = dis.readUTF();
144: }
145:
146: /**
147: * Serialize DateFormatSymbols object into output stream.
148: *
149: * @param out output stream
150: * @throws java.io.IOException is thrown if write fails
151: * @throws javax.microedition.global.ResourceException if resource
152: * can't be written
153: */
154: public void write(java.io.OutputStream out) throws IOException,
155: ResourceException {
156: DataOutputStream dous = new DataOutputStream(out);
157: writeStrings(eras, dous);
158: writeStrings(months, dous);
159: writeStrings(shortMonths, dous);
160: writeStrings(weekDays, dous);
161: writeStrings(shortWeekDays, dous);
162: writeStrings(ampms, dous);
163: writeStrings(patterns, dous);
164: dous.writeUTF(locale);
165: dous.flush();
166: }
167:
168: /**
169: * Write array helper. Writes string array.
170: *
171: * @param array strings to write
172: * @param dous output stream
173: * @throws IOException exception when write failed
174: */
175: protected void writeStrings(String[] array, DataOutputStream dous)
176: throws IOException {
177: for (int i = 0; i < array.length; i++) {
178: dous.writeUTF(array[i]);
179: }
180: }
181:
182: /**
183: * Read strings helper. Reads string array.
184: *
185: * @param array string array to read to
186: * @param dis input stream
187: * @throws IOException exception when read failed
188: */
189: protected void readStrings(String[] array, DataInputStream dis)
190: throws IOException {
191: for (int i = 0; i < array.length; i++) {
192: array[i] = dis.readUTF();
193: }
194: }
195: }
|