1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.joda.time.contrib.i18n;
17
18 import java.io.BufferedInputStream;
19 import java.io.ByteArrayInputStream;
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.InputStream;
23 import java.io.ObjectInputStream;
24
25 import org.joda.time.DateTimeZone;
26
27
28
29
30
31
32
33 public class CLDRTerritory extends Territory {
34
35
36 private static final byte[] cRawData = loadRawData();
37
38
39 private String iID;
40
41 private DateTimeZone[] iZones;
42
43 private int iFirstDOW;
44
45 private int iFirstBusinessDOW;
46
47 private int iLastBusinessDOW;
48
49 private int iFirstWeekendDOW;
50
51 private int iLastWeekendDOW;
52
53
54
55
56
57
58
59 private static byte[] loadRawData() {
60 String path = "org/joda/time/contrib/i18n/CLDRTerritoryData.dat";
61 InputStream baseStream = null;
62 ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
63 try {
64
65 baseStream = CLDRTerritory.class.getClassLoader().getResourceAsStream(path);
66 if (baseStream == null) {
67 throw new IOException("Resource not found " + path);
68 }
69 byte[] bytes = new byte[1024];
70 int result = 0;
71 while (result != -1) {
72 baos.write(bytes, 0, result);
73 result = baseStream.read(bytes, 0, 1024);
74 }
75 return baos.toByteArray();
76
77 } catch (IOException ex) {
78 throw new IllegalArgumentException("Territory data could not be loaded: " + ex.getMessage());
79 } finally {
80 if (baseStream != null) {
81 try {
82 baseStream.close();
83 } catch (IOException ex) {
84
85 }
86 }
87 }
88 }
89
90
91
92
93
94
95
96 CLDRTerritory(String id) {
97 super();
98 init(id);
99 }
100
101
102
103
104
105
106
107
108 private void init(String id) {
109 try {
110
111 ByteArrayInputStream bais = new ByteArrayInputStream(cRawData);
112 ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bais));
113 byte version = in.readByte();
114 if (version != 1) {
115 throw new IllegalArgumentException("Unknown file version: " + version);
116 }
117
118
119 String inputId = in.readUTF();
120 while (inputId.equals(id) == false) {
121 byte zoneCount = in.readByte();
122 for (int i = 0; i < zoneCount; i++) {
123 in.readUTF();
124 }
125 in.skip(5);
126 inputId = in.readUTF();
127 if (inputId.length() == 0) {
128 throw new IllegalArgumentException("Territory " + id + " could not be loaded");
129 }
130 }
131
132
133 iID = id;
134 byte zoneCount = in.readByte();
135 iZones = new DateTimeZone[zoneCount];
136 for (int i = 0; i < zoneCount; i++) {
137 String zoneID = in.readUTF();
138 try {
139 iZones[i] = DateTimeZone.forID(zoneID);
140 } catch (IllegalArgumentException ex) {
141
142 if (i == 0) {
143 throw ex;
144 }
145 }
146 }
147 iFirstDOW = in.readByte();
148 iFirstBusinessDOW = in.readByte();
149 iLastBusinessDOW = in.readByte();
150 iFirstWeekendDOW = in.readByte();
151 iLastWeekendDOW = in.readByte();
152
153 } catch (IOException ex) {
154 throw new IllegalArgumentException("Territory " + id + " could not be loaded: " + ex.getMessage());
155 }
156 }
157
158
159 public String getID() {
160 return iID;
161 }
162
163
164 public DateTimeZone[] getZones() {
165 return (DateTimeZone[]) iZones.clone();
166 }
167
168 public int getFirstDayOfWeek() {
169 return iFirstDOW;
170 }
171
172 public int getBusinessWeekStart() {
173 return iFirstBusinessDOW;
174 }
175
176 public int getBusinessWeekEnd() {
177 return iLastBusinessDOW;
178 }
179
180 public int getWeekendStart() {
181 return iFirstWeekendDOW;
182 }
183
184 public int getWeekendEnd() {
185 return iLastWeekendDOW;
186 }
187
188 }