harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From regi...@apache.org
Subject svn commit: r832292 - /harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Properties.java
Date Tue, 03 Nov 2009 03:31:56 GMT
Author: regisxu
Date: Tue Nov  3 03:31:55 2009
New Revision: 832292

URL: http://svn.apache.org/viewvc?rev=832292&view=rev
Log:
Apply patch for HARMONY-6368: [java6][classlib][luni]PropertiesTest of java6 branch failed

Modified:
    harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Properties.java

Modified: harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Properties.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Properties.java?rev=832292&r1=832291&r2=832292&view=diff
==============================================================================
--- harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Properties.java
(original)
+++ harmony/enhanced/classlib/branches/java6/modules/luni/src/main/java/java/util/Properties.java
Tue Nov  3 03:31:55 2009
@@ -21,6 +21,7 @@
 import java.io.InputStream;
 import java.io.BufferedReader;
 import java.io.BufferedInputStream;
+import java.io.InputStreamReader;
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.PrintStream;
@@ -258,7 +259,49 @@
 
     /**
      * Loads properties from the specified {@code InputStream}. The encoding is
-     * ISO8859-1. The {@code Properties} file is interpreted according to the
+     * ISO8859-1.
+     * 
+     * @param in
+     *            the {@code InputStream}.
+     * @throws IOException
+     *             if error occurs during reading from the {@code InputStream}.
+     */
+    public synchronized void load(InputStream in) throws IOException {
+        if (in == null) {
+            throw new NullPointerException();
+        }
+        BufferedInputStream bis = new BufferedInputStream(in);
+        bis.mark(Integer.MAX_VALUE);
+        boolean isEbcdic = isEbcdic(bis);
+        bis.reset();
+
+        if(!isEbcdic){
+            load(new InputStreamReader(bis, "ISO8859-1")); //$NON-NLS-1$
+        }else{
+            load(new InputStreamReader(bis)); //$NON-NLS-1$
+        }
+    }
+
+    private boolean isEbcdic(BufferedInputStream in) throws IOException{
+        byte b;
+        while ((b = (byte) in.read()) != -1) {
+            if (b == 0x23 || b == 0x0a || b == 0x3d) {//ascii: newline/#/=
+                return false;
+            }
+            if (b == 0x15) {//EBCDIC newline
+                return true;
+            }
+        }
+        //we found no ascii newline, '#', neither '=', relative safe to consider it
+        //as non-ascii, the only exception will be a single line with only key(no value and
'=')
+        //in this case, it should be no harm to read it in default charset
+        return false;
+    }
+    
+    /**
+     * Loads properties from the specified InputStream. The properties are of
+     * the form <code>key=value</code>, one property per line. It may be not
+     * encode as 'ISO-8859-1'.The {@code Properties} file is interpreted according to the
      * following rules:
      * <ul>
      * <li>Empty lines are ignored.</li>
@@ -278,183 +321,6 @@
      * "\!", "\#", "\t", "\b", "\f", and "&#92;uXXXX" (unicode character).</li>
      * </ul>
      * 
-     * @param in
-     *            the {@code InputStream}.
-     * @throws IOException
-     *             if error occurs during reading from the {@code InputStream}.
-     */
-    @SuppressWarnings("fallthrough")
-	public synchronized void load(InputStream in) throws IOException {
-		if (in == null) {
-			throw new NullPointerException();
-		}
-		int mode = NONE, unicode = 0, count = 0;
-		char nextChar, buf[] = new char[40];
-		int offset = 0, keyLength = -1, intVal;
-		boolean firstChar = true;
-                BufferedInputStream bis = new BufferedInputStream(in);
-
-		while (true) {
-			intVal = bis.read();
-            if (intVal == -1) {
-                // if mode is UNICODE but has less than 4 hex digits, should
-                // throw an IllegalArgumentException
-                // luni.08=Invalid Unicode sequence: expected format \\uxxxx
-                if (mode == UNICODE && count < 4) {
-                    throw new IllegalArgumentException(Messages.getString("luni.08")); //$NON-NLS-1$
-                }
-                // if mode is SLASH and no data is read, should append '\u0000'
-                // to buf
-                if (mode == SLASH) {
-                    buf[offset++] = '\u0000';
-                }
-                break;
-            }
-            nextChar = (char) (intVal & 0xff);
-
-			if (offset == buf.length) {
-				char[] newBuf = new char[buf.length * 2];
-				System.arraycopy(buf, 0, newBuf, 0, offset);
-				buf = newBuf;
-			}
-			if (mode == UNICODE) {
-				int digit = Character.digit(nextChar, 16);
-				if (digit >= 0) {
-					unicode = (unicode << 4) + digit;
-					if (++count < 4) {
-                        continue;
-                    }
-				} else {
-                    // luni.09=Invalid Unicode sequence: illegal character
-                    throw new IllegalArgumentException(Messages.getString("luni.09")); //$NON-NLS-1$
-                }
-				mode = NONE;
-				buf[offset++] = (char) unicode;
-				if (nextChar != '\n') {
-                    continue;
-                }
-			}
-			if (mode == SLASH) {
-				mode = NONE;
-				switch (nextChar) {
-				case '\r':
-					mode = CONTINUE; // Look for a following \n
-					continue;
-				case '\n':
-					mode = IGNORE; // Ignore whitespace on the next line
-					continue;
-				case 'b':
-					nextChar = '\b';
-					break;
-				case 'f':
-					nextChar = '\f';
-					break;
-				case 'n':
-					nextChar = '\n';
-					break;
-				case 'r':
-					nextChar = '\r';
-					break;
-				case 't':
-					nextChar = '\t';
-					break;
-				case 'u':
-					mode = UNICODE;
-					unicode = count = 0;
-					continue;
-				}
-			} else {
-				switch (nextChar) {
-				case '#':
-				case '!':
-					if (firstChar) {
-						while (true) {
-                            intVal = bis.read();
-                            if (intVal == -1) {
-                                break;
-                            }
-                            // & 0xff not required
-                            nextChar = (char) intVal;
-                            if (nextChar == '\r' || nextChar == '\n') {
-                                break;
-                            }
-						}
-						continue;
-					}
-					break;
-				case '\n':
-					if (mode == CONTINUE) { // Part of a \r\n sequence
-						mode = IGNORE; // Ignore whitespace on the next line
-						continue;
-					}
-				// fall into the next case
-				case '\r':
-					mode = NONE;
-					firstChar = true;
-					if (offset > 0) {
-						if (keyLength == -1) {
-							keyLength = offset;
-						}
-						String temp = new String(buf, 0, offset);
-						put(temp.substring(0, keyLength), temp
-								.substring(keyLength));
-					}
-					keyLength = -1;
-					offset = 0;
-					continue;
-				case '\\':
-					if (mode == KEY_DONE) {
-						keyLength = offset;
-					}
-					mode = SLASH;
-					continue;
-				case ':':
-				case '=':
-					if (keyLength == -1) { // if parsing the key
-						mode = NONE;
-						keyLength = offset;
-						continue;
-					}
-					break;
-				}
-				if (Character.isWhitespace(nextChar)) {
-					if (mode == CONTINUE) {
-                        mode = IGNORE;
-                    }
-					// if key length == 0 or value length == 0
-					if (offset == 0 || offset == keyLength || mode == IGNORE) {
-                        continue;
-                    }
-					if (keyLength == -1) { // if parsing the key
-						mode = KEY_DONE;
-						continue;
-					}
-				}
-				if (mode == IGNORE || mode == CONTINUE) {
-                    mode = NONE;
-                }
-			}
-			firstChar = false;
-			if (mode == KEY_DONE) {
-				keyLength = offset;
-				mode = NONE;
-			}
-			buf[offset++] = nextChar;
-		}
-        if(keyLength==-1 && offset>0){
-            keyLength = offset;
-        }
-		if (keyLength >= 0) {
-			String temp = new String(buf, 0, offset);
-			put(temp.substring(0, keyLength), temp.substring(keyLength));
-		}
-	}
-    
-    /**
-     * Loads properties from the specified InputStream. The properties are of
-     * the form <code>key=value</code>, one property per line. It may be not
-     * encode as 'ISO-8859-1'.
-     * 
      * @param reader
      *            the input reader
      * @throws IOException



Mime
View raw message