118
119 /** use serialVersionUID from JDK 1.0.2 for interoperability */
120 private static final long serialVersionUID = -6849794470754667710L;
121
122 /**
123 * Class String is special cased within the Serialization Stream Protocol.
124 *
125 * A String instance is written into an ObjectOutputStream according to
126 * <a href="{@docRoot}/../platform/serialization/spec/output.html">
127 * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
128 */
129 private static final ObjectStreamField[] serialPersistentFields =
130 new ObjectStreamField[0];
131
132 /**
133 * Initializes a newly created {@code String} object so that it represents
134 * an empty character sequence. Note that use of this constructor is
135 * unnecessary since Strings are immutable.
136 */
137 public String() {
138 this.value = new char[0];
139 }
140
141 /**
142 * Initializes a newly created {@code String} object so that it represents
143 * the same sequence of characters as the argument; in other words, the
144 * newly created string is a copy of the argument string. Unless an
145 * explicit copy of {@code original} is needed, use of this constructor is
146 * unnecessary since Strings are immutable.
147 *
148 * @param original
149 * A {@code String}
150 */
151 public String(String original) {
152 this.value = original.value;
153 this.hash = original.hash;
154 }
155
156 /**
157 * Allocates a new {@code String} so that it represents the sequence of
158 * characters currently contained in the character array argument. The
174 * subarray are copied; subsequent modification of the character array does
175 * not affect the newly created string.
176 *
177 * @param value
178 * Array that is the source of characters
179 *
180 * @param offset
181 * The initial offset
182 *
183 * @param count
184 * The length
185 *
186 * @throws IndexOutOfBoundsException
187 * If {@code offset} is negative, {@code count} is negative, or
188 * {@code offset} is greater than {@code value.length - count}
189 */
190 public String(char value[], int offset, int count) {
191 if (offset < 0) {
192 throw new StringIndexOutOfBoundsException(offset);
193 }
194 if (count < 0) {
195 throw new StringIndexOutOfBoundsException(count);
196 }
197 // Note: offset or count might be near -1>>>1.
198 if (offset > value.length - count) {
199 throw new StringIndexOutOfBoundsException(offset + count);
200 }
201 this.value = Arrays.copyOfRange(value, offset, offset+count);
202 }
203
204 /**
205 * Allocates a new {@code String} that contains characters from a subarray
206 * of the <a href="Character.html#unicode">Unicode code point</a> array
207 * argument. The {@code offset} argument is the index of the first code
208 * point of the subarray and the {@code count} argument specifies the
209 * length of the subarray. The contents of the subarray are converted to
210 * {@code char}s; subsequent modification of the {@code int} array does not
211 * affect the newly created string.
212 *
213 * @param codePoints
214 * Array that is the source of Unicode code points
216 * @param offset
217 * The initial offset
218 *
219 * @param count
220 * The length
221 *
222 * @throws IllegalArgumentException
223 * If any invalid Unicode code point is found in {@code
224 * codePoints}
225 *
226 * @throws IndexOutOfBoundsException
227 * If {@code offset} is negative, {@code count} is negative, or
228 * {@code offset} is greater than {@code codePoints.length - count}
229 *
230 * @since 1.5
231 */
232 public String(int[] codePoints, int offset, int count) {
233 if (offset < 0) {
234 throw new StringIndexOutOfBoundsException(offset);
235 }
236 if (count < 0) {
237 throw new StringIndexOutOfBoundsException(count);
238 }
239 // Note: offset or count might be near -1>>>1.
240 if (offset > codePoints.length - count) {
241 throw new StringIndexOutOfBoundsException(offset + count);
242 }
243
244 final int end = offset + count;
245
246 // Pass 1: Compute precise size of char[]
247 int n = count;
248 for (int i = offset; i < end; i++) {
249 int c = codePoints[i];
250 if (Character.isBmpCodePoint(c))
251 continue;
252 else if (Character.isValidCodePoint(c))
253 n++;
254 else throw new IllegalArgumentException(Integer.toString(c));
255 }
256
|
118
119 /** use serialVersionUID from JDK 1.0.2 for interoperability */
120 private static final long serialVersionUID = -6849794470754667710L;
121
122 /**
123 * Class String is special cased within the Serialization Stream Protocol.
124 *
125 * A String instance is written into an ObjectOutputStream according to
126 * <a href="{@docRoot}/../platform/serialization/spec/output.html">
127 * Object Serialization Specification, Section 6.2, "Stream Elements"</a>
128 */
129 private static final ObjectStreamField[] serialPersistentFields =
130 new ObjectStreamField[0];
131
132 /**
133 * Initializes a newly created {@code String} object so that it represents
134 * an empty character sequence. Note that use of this constructor is
135 * unnecessary since Strings are immutable.
136 */
137 public String() {
138 this.value = "".value;
139 }
140
141 /**
142 * Initializes a newly created {@code String} object so that it represents
143 * the same sequence of characters as the argument; in other words, the
144 * newly created string is a copy of the argument string. Unless an
145 * explicit copy of {@code original} is needed, use of this constructor is
146 * unnecessary since Strings are immutable.
147 *
148 * @param original
149 * A {@code String}
150 */
151 public String(String original) {
152 this.value = original.value;
153 this.hash = original.hash;
154 }
155
156 /**
157 * Allocates a new {@code String} so that it represents the sequence of
158 * characters currently contained in the character array argument. The
174 * subarray are copied; subsequent modification of the character array does
175 * not affect the newly created string.
176 *
177 * @param value
178 * Array that is the source of characters
179 *
180 * @param offset
181 * The initial offset
182 *
183 * @param count
184 * The length
185 *
186 * @throws IndexOutOfBoundsException
187 * If {@code offset} is negative, {@code count} is negative, or
188 * {@code offset} is greater than {@code value.length - count}
189 */
190 public String(char value[], int offset, int count) {
191 if (offset < 0) {
192 throw new StringIndexOutOfBoundsException(offset);
193 }
194 if (count <= 0) {
195 if (count == 0) {
196 this.value = "".value;
197 return;
198 }
199 throw new StringIndexOutOfBoundsException(count);
200 }
201 // Note: offset or count might be near -1>>>1.
202 if (offset > value.length - count) {
203 throw new StringIndexOutOfBoundsException(offset + count);
204 }
205 this.value = Arrays.copyOfRange(value, offset, offset+count);
206 }
207
208 /**
209 * Allocates a new {@code String} that contains characters from a subarray
210 * of the <a href="Character.html#unicode">Unicode code point</a> array
211 * argument. The {@code offset} argument is the index of the first code
212 * point of the subarray and the {@code count} argument specifies the
213 * length of the subarray. The contents of the subarray are converted to
214 * {@code char}s; subsequent modification of the {@code int} array does not
215 * affect the newly created string.
216 *
217 * @param codePoints
218 * Array that is the source of Unicode code points
220 * @param offset
221 * The initial offset
222 *
223 * @param count
224 * The length
225 *
226 * @throws IllegalArgumentException
227 * If any invalid Unicode code point is found in {@code
228 * codePoints}
229 *
230 * @throws IndexOutOfBoundsException
231 * If {@code offset} is negative, {@code count} is negative, or
232 * {@code offset} is greater than {@code codePoints.length - count}
233 *
234 * @since 1.5
235 */
236 public String(int[] codePoints, int offset, int count) {
237 if (offset < 0) {
238 throw new StringIndexOutOfBoundsException(offset);
239 }
240 if (count <= 0) {
241 if (count == 0) {
242 this.value = "".value;
243 return;
244 }
245 throw new StringIndexOutOfBoundsException(count);
246 }
247 // Note: offset or count might be near -1>>>1.
248 if (offset > codePoints.length - count) {
249 throw new StringIndexOutOfBoundsException(offset + count);
250 }
251
252 final int end = offset + count;
253
254 // Pass 1: Compute precise size of char[]
255 int n = count;
256 for (int i = offset; i < end; i++) {
257 int c = codePoints[i];
258 if (Character.isBmpCodePoint(c))
259 continue;
260 else if (Character.isValidCodePoint(c))
261 n++;
262 else throw new IllegalArgumentException(Integer.toString(c));
263 }
264
|