diff options
Diffstat (limited to 'libjava/classpath/java')
-rw-r--r-- | libjava/classpath/java/lang/String.java | 123 | ||||
-rw-r--r-- | libjava/classpath/java/text/MessageFormat.java | 51 | ||||
-rw-r--r-- | libjava/classpath/java/text/NumberFormat.java | 1 | ||||
-rw-r--r-- | libjava/classpath/java/text/RuleBasedCollator.java | 104 | ||||
-rw-r--r-- | libjava/classpath/java/text/SimpleDateFormat.java | 24 |
5 files changed, 190 insertions, 113 deletions
diff --git a/libjava/classpath/java/lang/String.java b/libjava/classpath/java/lang/String.java index 95c88399d3c..c8c6cb9627a 100644 --- a/libjava/classpath/java/lang/String.java +++ b/libjava/classpath/java/lang/String.java @@ -337,9 +337,59 @@ public final class String * @throws Error if the decoding fails * @since 1.1 */ - public String(byte[] data, int offset, int count, String encoding) + public String(byte[] data, int offset, int count, final String encoding) throws UnsupportedEncodingException { + this(data, offset, count, stringToCharset(encoding)); + } + + /** + * Wrapper method to convert exceptions resulting from + * the selection of a {@link java.nio.charset.Charset} based on + * a String. + * + * @throws UnsupportedEncodingException if encoding is not found + */ + private static final Charset stringToCharset(final String encoding) + throws UnsupportedEncodingException + { + try + { + return Charset.forName(encoding); + } + catch(IllegalCharsetNameException e) + { + throw new UnsupportedEncodingException("Encoding: "+encoding+ + " not found."); + } + catch(UnsupportedCharsetException e) + { + throw new UnsupportedEncodingException("Encoding: "+encoding+ + " not found."); + } + } + + /** + * Creates a new String using the portion of the byte array starting at the + * offset and ending at offset + count. Uses the specified encoding type + * to decode the byte array, so the resulting string may be longer or + * shorter than the byte array. For more decoding control, use + * {@link java.nio.charset.CharsetDecoder}, and for valid character sets, + * see {@link java.nio.charset.Charset}. Malformed input and unmappable + * character sequences are replaced with the default replacement string + * provided by the {@link java.nio.charset.Charset}. + * + * @param data byte array to copy + * @param offset the offset to start at + * @param count the number of bytes in the array to use + * @param encoding the encoding to use + * @throws NullPointerException if data or encoding is null + * @throws IndexOutOfBoundsException if offset or count is incorrect + * (while unspecified, this is a StringIndexOutOfBoundsException) + * @since 1.6 + */ + public String(byte[] data, int offset, int count, Charset encoding) + { if (offset < 0) throw new StringIndexOutOfBoundsException("offset: " + offset); if (count < 0) @@ -350,7 +400,7 @@ public final class String + (offset + count)); try { - CharsetDecoder csd = Charset.forName(encoding).newDecoder(); + CharsetDecoder csd = encoding.newDecoder(); csd.onMalformedInput(CodingErrorAction.REPLACE); csd.onUnmappableCharacter(CodingErrorAction.REPLACE); CharBuffer cbuf = csd.decode(ByteBuffer.wrap(data, offset, count)); @@ -366,16 +416,12 @@ public final class String this.offset = 0; this.count = value.length; } - } catch(CharacterCodingException e){ - throw new UnsupportedEncodingException("Encoding: "+encoding+ - " not found."); - } catch(IllegalCharsetNameException e){ - throw new UnsupportedEncodingException("Encoding: "+encoding+ - " not found."); - } catch(UnsupportedCharsetException e){ - throw new UnsupportedEncodingException("Encoding: "+encoding+ - " not found."); - } + } + catch(CharacterCodingException e) + { + // This shouldn't ever happen. + throw (InternalError) new InternalError().initCause(e); + } } /** @@ -402,6 +448,26 @@ public final class String } /** + * Creates a new String using the byte array. Uses the specified encoding + * type to decode the byte array, so the resulting string may be longer or + * shorter than the byte array. For more decoding control, use + * {@link java.nio.charset.CharsetDecoder}, and for valid character sets, + * see {@link java.nio.charset.Charset}. Malformed input and unmappable + * character sequences are replaced with the default replacement string + * provided by the {@link java.nio.charset.Charset}. + * + * @param data byte array to copy + * @param encoding the name of the encoding to use + * @throws NullPointerException if data or encoding is null + * @see #String(byte[], int, int, java.nio.Charset) + * @since 1.6 + */ + public String(byte[] data, Charset encoding) + { + this(data, 0, data.length, encoding); + } + + /** * Creates a new String using the portion of the byte array starting at the * offset and ending at offset + count. Uses the encoding of the platform's * default charset, so the resulting string may be longer or shorter than @@ -726,11 +792,30 @@ public final class String * @throws UnsupportedEncodingException if encoding is not supported * @since 1.1 */ - public byte[] getBytes(String enc) throws UnsupportedEncodingException + public byte[] getBytes(final String enc) + throws UnsupportedEncodingException + { + return getBytes(stringToCharset(enc)); + } + + /** + * Converts the Unicode characters in this String to a byte array. Uses the + * specified encoding method, so the result may be longer or shorter than + * the String. For more encoding control, use + * {@link java.nio.charset.CharsetEncoder}, and for valid character sets, + * see {@link java.nio.charset.Charset}. Unsupported characters get + * replaced by the {@link java.nio.charset.Charset}'s default replacement. + * + * @param enc encoding name + * @return the resulting byte array + * @throws NullPointerException if enc is null + * @since 1.6 + */ + public byte[] getBytes(Charset enc) { try { - CharsetEncoder cse = Charset.forName(enc).newEncoder(); + CharsetEncoder cse = enc.newEncoder(); cse.onMalformedInput(CodingErrorAction.REPLACE); cse.onUnmappableCharacter(CodingErrorAction.REPLACE); ByteBuffer bbuf = cse.encode(CharBuffer.wrap(value, offset, count)); @@ -742,16 +827,6 @@ public final class String bbuf.get(bytes); return bytes; } - catch(IllegalCharsetNameException e) - { - throw new UnsupportedEncodingException("Encoding: " + enc - + " not found."); - } - catch(UnsupportedCharsetException e) - { - throw new UnsupportedEncodingException("Encoding: " + enc - + " not found."); - } catch(CharacterCodingException e) { // This shouldn't ever happen. diff --git a/libjava/classpath/java/text/MessageFormat.java b/libjava/classpath/java/text/MessageFormat.java index c5579bff1f0..700b187fefd 100644 --- a/libjava/classpath/java/text/MessageFormat.java +++ b/libjava/classpath/java/text/MessageFormat.java @@ -38,13 +38,17 @@ exception statement from your version. */ package java.text; +import gnu.java.lang.CPStringBuilder; + import gnu.java.text.FormatCharacterIterator; import java.io.InvalidObjectException; + +import java.util.ArrayList; import java.util.Date; import java.util.HashMap; +import java.util.List; import java.util.Locale; -import java.util.Vector; public class MessageFormat extends Format { @@ -69,7 +73,7 @@ public class MessageFormat extends Format // Argument will be checked to make sure it is an instance of this // class. - Class formatClass; + Class<?> formatClass; // Formatter type. String type; @@ -95,12 +99,7 @@ public class MessageFormat extends Format else if (style.equals("percent")) format = NumberFormat.getPercentInstance(loc); else if (style.equals("integer")) - { - NumberFormat nf = NumberFormat.getNumberInstance(loc); - nf.setMaximumFractionDigits(0); - nf.setGroupingUsed(false); - format = nf; - } + format = NumberFormat.getIntegerInstance(loc); else { format = NumberFormat.getNumberInstance(loc); @@ -165,6 +164,7 @@ public class MessageFormat extends Format public static final MessageFormat.Field ARGUMENT = new MessageFormat.Field("argument"); // For deserialization + @SuppressWarnings("unused") private Field() { super(""); @@ -194,7 +194,7 @@ public class MessageFormat extends Format // Helper that returns the text up to the next format opener. The // text is put into BUFFER. Returns index of character after end of // string. Throws IllegalArgumentException on error. - private static int scanString(String pat, int index, StringBuilder buffer) + private static int scanString(String pat, int index, CPStringBuilder buffer) { int max = pat.length(); buffer.setLength(0); @@ -234,7 +234,7 @@ public class MessageFormat extends Format // This helper retrieves a single part of a format element. Returns // the index of the terminating character. private static int scanFormatElement(String pat, int index, - StringBuilder buffer, char term) + CPStringBuilder buffer, char term) { int max = pat.length(); buffer.setLength(0); @@ -281,11 +281,11 @@ public class MessageFormat extends Format // This is used to parse a format element and whatever non-format // text might trail it. - private static int scanFormat(String pat, int index, StringBuilder buffer, - Vector elts, Locale locale) + private static int scanFormat(String pat, int index, CPStringBuilder buffer, + List<MessageFormatElement> elts, Locale locale) { MessageFormatElement mfe = new MessageFormatElement (); - elts.addElement(mfe); + elts.add(mfe); int max = pat.length(); @@ -342,17 +342,16 @@ public class MessageFormat extends Format { pattern = newPattern; - StringBuilder tempBuffer = new StringBuilder (); + CPStringBuilder tempBuffer = new CPStringBuilder (); int index = scanString (newPattern, 0, tempBuffer); leader = tempBuffer.toString(); - Vector elts = new Vector (); + List<MessageFormatElement> elts = new ArrayList<MessageFormatElement>(); while (index < newPattern.length()) index = scanFormat (newPattern, index, tempBuffer, elts, locale); - elements = new MessageFormatElement[elts.size()]; - elts.copyInto(elements); + elements = elts.toArray(new MessageFormatElement[elts.size()]); } /** @@ -494,7 +493,8 @@ public class MessageFormat extends Format if (output_iterator != null) { - HashMap hash_argument = new HashMap(); + HashMap<MessageFormat.Field, Integer> hash_argument = + new HashMap<MessageFormat.Field, Integer>(); int position = output_iterator.getEndIndex(); hash_argument.put (MessageFormat.Field.ARGUMENT, @@ -613,7 +613,7 @@ public class MessageFormat extends Format } index += leader.length(); - Vector results = new Vector (elements.length, 1); + ArrayList<Object> results = new ArrayList<Object>(elements.length); // Now check each format. for (int i = 0; i < elements.length; ++i) { @@ -681,15 +681,18 @@ public class MessageFormat extends Format } if (elements[i].argNumber >= results.size()) - results.setSize(elements[i].argNumber + 1); - results.setElementAt(value, elements[i].argNumber); + { + // Emulate padding behaviour of Vector.setSize() with ArrayList + results.ensureCapacity(elements[i].argNumber + 1); + for (int a = results.size(); a <= elements[i].argNumber; ++a) + results.add(a, null); + } + results.set(elements[i].argNumber, value); index += elements[i].trailer.length(); } - Object[] r = new Object[results.size()]; - results.copyInto(r); - return r; + return results.toArray(new Object[results.size()]); } public Object[] parse (String sourceStr) throws ParseException diff --git a/libjava/classpath/java/text/NumberFormat.java b/libjava/classpath/java/text/NumberFormat.java index 0a436d00439..294f8917c14 100644 --- a/libjava/classpath/java/text/NumberFormat.java +++ b/libjava/classpath/java/text/NumberFormat.java @@ -177,6 +177,7 @@ public abstract class NumberFormat extends Format implements Cloneable * This constructor is only used by the deserializer. Without it, * it would fail to construct a valid object. */ + @SuppressWarnings("unused") private Field() { super(""); diff --git a/libjava/classpath/java/text/RuleBasedCollator.java b/libjava/classpath/java/text/RuleBasedCollator.java index fdd1446cc0c..b761039ad80 100644 --- a/libjava/classpath/java/text/RuleBasedCollator.java +++ b/libjava/classpath/java/text/RuleBasedCollator.java @@ -151,13 +151,13 @@ public class RuleBasedCollator extends Collator */ static final class CollationElement { - String key; - int primary; - short secondary; - short tertiary; - short equality; - boolean ignore; - String expansion; + final String key; + final int primary; + final short secondary; + final short tertiary; + final short equality; + final boolean ignore; + final String expansion; CollationElement(String key, int primary, short secondary, short tertiary, short equality, String expansion, boolean ignore) @@ -185,7 +185,7 @@ public class RuleBasedCollator extends Collator * {@link #mergeRules(int,java.lang.String,java.util.ArrayList,java.util.ArrayList)}) * as a temporary state while merging two sets of instructions. */ - static final class CollationSorter + private static final class CollationSorter { static final int GREATERP = 0; static final int GREATERS = 1; @@ -194,29 +194,39 @@ public class RuleBasedCollator extends Collator static final int RESET = 4; static final int INVERSE_SECONDARY = 5; - int comparisonType; - String textElement; - int hashText; - int offset; - boolean ignore; + final int comparisonType; + final String textElement; + final int hashText; + final int offset; + final boolean ignore; String expansionOrdering; + + private CollationSorter(final int comparisonType, final String textElement, + final int offset, final boolean ignore) + { + this.comparisonType = comparisonType; + this.textElement = textElement; + this.offset = offset; + this.ignore = ignore; + hashText = textElement.hashCode(); + } } /** - * This the the original rule string. + * This is the original rule string. */ private String rules; /** * This is the table of collation element values */ - private Object[] ce_table; + private CollationElement[] ce_table; /** * Quick-prefix finder. */ - HashMap prefix_tree; + HashMap<String,CollationElement> prefix_tree; /** * This is the value of the last sequence entered into @@ -306,7 +316,8 @@ public class RuleBasedCollator extends Collator * @param patch Rules to be merged into the repository. * @throws ParseException if it is impossible to find an anchor point for the new rules. */ - private void mergeRules(int offset, String starter, ArrayList main, ArrayList patch) + private void mergeRules(int offset, String starter, ArrayList<CollationSorter> main, + ArrayList<CollationSorter> patch) throws ParseException { int insertion_point = -1; @@ -324,8 +335,8 @@ public class RuleBasedCollator extends Collator while (j < main.size()) { - CollationSorter rule1 = (CollationSorter) patch.get(i); - CollationSorter rule2 = (CollationSorter) main.get(j); + CollationSorter rule1 = patch.get(i); + CollationSorter rule2 = main.get(j); if (rule1.textElement.equals(rule2.textElement)) main.remove(j); @@ -337,7 +348,7 @@ public class RuleBasedCollator extends Collator // Find the insertion point... O(N) for (int i = 0; i < main.size(); i++) { - CollationSorter sorter = (CollationSorter) main.get(i); + CollationSorter sorter = main.get(i); int length = findPrefixLength(starter, sorter.textElement); if (length > max_length) @@ -363,9 +374,7 @@ public class RuleBasedCollator extends Collator * sequence. The rest of the subsequence must be appended * to the end of the sequence. */ - CollationSorter sorter = (CollationSorter) patch.get(0); - CollationSorter expansionPrefix = - (CollationSorter) main.get(insertion_point-1); + CollationSorter sorter = patch.get(0); sorter.expansionOrdering = starter.substring(max_length); // Skip the first good prefix element @@ -398,7 +407,7 @@ public class RuleBasedCollator extends Collator * @throws ParseException if something turned wrong during the parsing. To get details * decode the message. */ - private int subParseString(boolean stop_on_reset, ArrayList v, + private int subParseString(boolean stop_on_reset, ArrayList<CollationSorter> v, int base_offset, String rules) throws ParseException { @@ -508,7 +517,7 @@ main_parse_loop: * indicated by the text element. */ String subrules = rules.substring(i); - ArrayList sorted_rules = new ArrayList(); + ArrayList<CollationSorter> sorted_rules = new ArrayList<CollationSorter>(); int idx; // Parse the subrules but do not iterate through all @@ -533,16 +542,12 @@ main_parse_loop: break main_parse_loop; } - CollationSorter sorter = new CollationSorter(); - + String textElement = sb.toString(); if (operator == CollationSorter.GREATERP) ignoreChars = false; - - sorter.comparisonType = operator; - sorter.textElement = sb.toString(); - sorter.hashText = sorter.textElement.hashCode(); - sorter.offset = base_offset+rules.length(); - sorter.ignore = ignoreChars; + CollationSorter sorter = new CollationSorter(operator, textElement, + base_offset + rules.length(), + ignoreChars); sb.setLength(0); v.add(sorter); @@ -551,7 +556,6 @@ main_parse_loop: if (operator >= 0) { - CollationSorter sorter = new CollationSorter(); int pos = rules.length() + base_offset; if ((sb.length() != 0 && nextIsModifier) @@ -561,11 +565,8 @@ main_parse_loop: if (operator == CollationSorter.GREATERP) ignoreChars = false; - sorter.comparisonType = operator; - sorter.textElement = sb.toString(); - sorter.hashText = sorter.textElement.hashCode(); - sorter.offset = base_offset+pos; - sorter.ignore = ignoreChars; + CollationSorter sorter = new CollationSorter(operator, sb.toString(), + base_offset+pos, ignoreChars); v.add(sorter); } @@ -593,10 +594,10 @@ main_parse_loop: * @throws ParseException if something turned wrong during the parsing. To get details * decode the message. */ - private ArrayList parseString(String rules) + private ArrayList<CollationSorter> parseString(String rules) throws ParseException { - ArrayList v = new ArrayList(); + ArrayList<CollationSorter> v = new ArrayList<CollationSorter>(); // result of the first subParseString is not absolute (may be -1 or a // positive integer). But we do not care. @@ -612,7 +613,7 @@ main_parse_loop: * @param parsedElements Parsed instructions stored in a ArrayList. * @throws ParseException if the order of the instructions are not valid. */ - private void buildCollationVector(ArrayList parsedElements) + private void buildCollationVector(ArrayList<CollationSorter> parsedElements) throws ParseException { int primary_seq = 0; @@ -624,14 +625,13 @@ main_parse_loop: final boolean DECREASING = false; final boolean INCREASING = true; boolean secondaryType = INCREASING; - ArrayList v = new ArrayList(); + ArrayList<CollationElement> v = new ArrayList<CollationElement>(); // elts is completely sorted. element_loop: for (int i = 0; i < parsedElements.size(); i++) { - CollationSorter elt = (CollationSorter) parsedElements.get(i); - boolean ignoreChar = false; + CollationSorter elt = parsedElements.get(i); switch (elt.comparisonType) { @@ -686,7 +686,7 @@ element_loop: this.inverseAccentComparison = inverseComparisons; - ce_table = v.toArray(); + ce_table = v.toArray(new CollationElement[v.size()]); last_primary_value = primary_seq+1; last_tertiary_value = last_tertiary_seq+1; @@ -699,11 +699,11 @@ element_loop: */ private void buildPrefixAccess() { - prefix_tree = new HashMap(); + prefix_tree = new HashMap<String,CollationElement>(); for (int i = 0; i < ce_table.length; i++) { - CollationElement e = (CollationElement) ce_table[i]; + CollationElement e = ce_table[i]; prefix_tree.put(e.key, e); } @@ -941,7 +941,7 @@ element_loop: public CollationKey getCollationKey(String source) { CollationElementIterator cei = getCollationElementIterator(source); - ArrayList vect = new ArrayList(); + ArrayList<Integer> vect = new ArrayList<Integer>(); int ord = cei.next(); cei.reset(); //set to start of string @@ -969,16 +969,16 @@ element_loop: break; } - vect.add(new Integer(ord)); + vect.add(Integer.valueOf(ord)); ord = cei.next(); //increment to next key } - Object[] objarr = vect.toArray(); + Integer[] objarr = vect.toArray(new Integer[vect.size()]); byte[] key = new byte[objarr.length * 4]; for (int i = 0; i < objarr.length; i++) { - int j = ((Integer) objarr[i]).intValue(); + int j = objarr[i].intValue(); key [i * 4] = (byte) ((j & 0xFF000000) >> 24); key [i * 4 + 1] = (byte) ((j & 0x00FF0000) >> 16); key [i * 4 + 2] = (byte) ((j & 0x0000FF00) >> 8); diff --git a/libjava/classpath/java/text/SimpleDateFormat.java b/libjava/classpath/java/text/SimpleDateFormat.java index 4dcda4f487c..b09c7caa28d 100644 --- a/libjava/classpath/java/text/SimpleDateFormat.java +++ b/libjava/classpath/java/text/SimpleDateFormat.java @@ -159,13 +159,13 @@ public class SimpleDateFormat extends DateFormat } /** - * A list of <code>CompiledField</code>s, + * A list of <code>CompiledField</code>s and {@code String}s * representing the compiled version of the pattern. * * @see CompiledField * @serial Ignored. */ - private transient ArrayList tokens; + private transient ArrayList<Object> tokens; /** * The localised data used in formatting, @@ -235,8 +235,8 @@ public class SimpleDateFormat extends DateFormat */ private static final long serialVersionUID = 4774881970558875024L; - // This string is specified in the Java class libraries. - private static final String standardChars = "GyMdkHmsSEDFwWahKzZ"; + // This string is specified in the root of the CLDR. + private static final String standardChars = "GyMdkHmsSEDFwWahKzYeugAZvcL"; /** * Represents the position of the RFC822 timezone pattern character @@ -244,7 +244,7 @@ public class SimpleDateFormat extends DateFormat * U.S. locale, this is 'Z'. The value is the offset of the current * time from GMT e.g. -0500 would be five hours prior to GMT. */ - private static final int RFC822_TIMEZONE_FIELD = 18; + private static final int RFC822_TIMEZONE_FIELD = 23; /** * Reads the serialized version of this object. @@ -274,7 +274,7 @@ public class SimpleDateFormat extends DateFormat set2DigitYearStart(defaultCenturyStart); // Set up items normally taken care of by the constructor. - tokens = new ArrayList(); + tokens = new ArrayList<Object>(); try { compileFormat(pattern); @@ -416,7 +416,7 @@ public class SimpleDateFormat extends DateFormat Locale locale = Locale.getDefault(); calendar = new GregorianCalendar(locale); computeCenturyStart(); - tokens = new ArrayList(); + tokens = new ArrayList<Object>(); formatData = new DateFormatSymbols(locale); pattern = (formatData.dateFormats[DEFAULT] + ' ' + formatData.timeFormats[DEFAULT]); @@ -454,7 +454,7 @@ public class SimpleDateFormat extends DateFormat super(); calendar = new GregorianCalendar(locale); computeCenturyStart(); - tokens = new ArrayList(); + tokens = new ArrayList<Object>(); formatData = new DateFormatSymbols(locale); compileFormat(pattern); this.pattern = pattern; @@ -479,7 +479,7 @@ public class SimpleDateFormat extends DateFormat super(); calendar = new GregorianCalendar(); computeCenturyStart (); - tokens = new ArrayList(); + tokens = new ArrayList<Object>(); if (formatData == null) throw new NullPointerException("formatData"); this.formatData = formatData; @@ -524,7 +524,7 @@ public class SimpleDateFormat extends DateFormat */ public void applyPattern(String pattern) { - tokens = new ArrayList(); + tokens.clear(); compileFormat(pattern); this.pattern = pattern; } @@ -697,11 +697,10 @@ public class SimpleDateFormat extends DateFormat private void formatWithAttribute(Date date, FormatBuffer buffer, FieldPosition pos) { String temp; - AttributedCharacterIterator.Attribute attribute; calendar.setTime(date); // go through vector, filling in fields where applicable, else toString - Iterator iter = tokens.iterator(); + Iterator<Object> iter = tokens.iterator(); while (iter.hasNext()) { Object o = iter.next(); @@ -910,7 +909,6 @@ public class SimpleDateFormat extends DateFormat char ch = pattern.charAt(fmt_index); if (ch == '\'') { - int index = pos.getIndex(); if (fmt_index < fmt_max - 1 && pattern.charAt(fmt_index + 1) == '\'') { |