sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1407991 - /sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/util/resources/IndexedResourceCompiler.java
Date Sun, 11 Nov 2012 14:05:43 GMT
Author: desruisseaux
Date: Sun Nov 11 14:05:42 2012
New Revision: 1407991

URL: http://svn.apache.org/viewvc?rev=1407991&view=rev
Log:
Use try-with-resource.

Modified:
    sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/util/resources/IndexedResourceCompiler.java

Modified: sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/util/resources/IndexedResourceCompiler.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/util/resources/IndexedResourceCompiler.java?rev=1407991&r1=1407990&r2=1407991&view=diff
==============================================================================
--- sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/util/resources/IndexedResourceCompiler.java
(original)
+++ sis/branches/JDK7/sis-build-helper/src/main/java/org/apache/sis/util/resources/IndexedResourceCompiler.java
Sun Nov 11 14:05:42 2012
@@ -271,10 +271,11 @@ class IndexedResourceCompiler implements
      * @throws IOException if the file can not be read.
      */
     private static Properties loadRawProperties(final File file) throws IOException {
-        final InputStream input = new FileInputStream(file);
-        final Properties properties = new Properties();
-        properties.load(input);
-        input.close();
+        final Properties properties;
+        try (InputStream input = new FileInputStream(file)) {
+            properties = new Properties();
+            properties.load(input);
+        }
         return properties;
     }
 
@@ -438,13 +439,13 @@ search: for (int i=0; i<buffer.length();
             throw new IOException("Can't create the " + directory + " directory.");
         }
         final int count = allocatedIDs.isEmpty() ? 0 : Collections.max(allocatedIDs.keySet())
+ 1;
-        final DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));
-        out.writeInt(count);
-        for (int i=0; i<count; i++) {
-            final String value = (String) resources.get(allocatedIDs.get(i));
-            out.writeUTF((value != null) ? value : "");
+        try (DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file))))
{
+            out.writeInt(count);
+            for (int i=0; i<count; i++) {
+                final String value = (String) resources.get(allocatedIDs.get(i));
+                out.writeUTF((value != null) ? value : "");
+            }
         }
-        out.close();
     }
 
     /**
@@ -467,7 +468,6 @@ search: for (int i=0; i<buffer.length();
         if (!file.getParentFile().isDirectory()) {
             throw new FileNotFoundException("Parent directory not found for " + file);
         }
-        final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),
JAVA_ENCODING));
         final String lineSeparator = System.getProperty("line.separator", "\n");
         final StringBuilder buffer = new StringBuilder(4096);
         /*
@@ -476,106 +476,108 @@ search: for (int i=0; i<buffer.length();
          * on the same line than the class declaration).
          */
         final Pattern classKeys = Pattern.compile("[\\s\\w]*class\\s+" + KEYS_INNER_CLASS
+ "\\s*\\{");
-        String line;
-        do {
-            line = in.readLine();
-            if (line == null) {
-                in.close();
-                throw new EOFException(file.toString());
-            }
-            buffer.append(line).append(lineSeparator);
-        } while (!classKeys.matcher(line).matches());
-        /*
-         * Starting from this point, the content that we are going to write in the buffer
-         * may be different than the file content.  Remember the buffer position in order
-         * to allow us to compare the buffer with the file content.
-         *
-         * We stop reading the file for now. We will continue reading the file after the
-         * 'for' loop below. Instead, we now write the constructor followed by keys values.
-         */
-        int startLineToCompare = buffer.length();
-        buffer.append(KEY_MARGIN).append("private ").append(KEYS_INNER_CLASS).append("()
{").append(lineSeparator)
-              .append(KEY_MARGIN).append('}').append(lineSeparator);
-        final Map.Entry<?,?>[] entries = allocatedIDs.entrySet().toArray(new Map.Entry<?,?>[allocatedIDs.size()]);
-        Arrays.sort(entries, this);
-        for (int i=0; i<entries.length; i++) {
-            buffer.append(lineSeparator);
-            final String key = (String) entries[i].getValue();
-            final String ID  = entries[i].getKey().toString();
-            String message = (String) resources.get(key);
-            if (message != null) {
-                message = message.replace('\t', ' ');
-                buffer.append(KEY_MARGIN).append("/**").append(lineSeparator);
-                while (((message=message.trim()).length()) != 0) {
-                    buffer.append(KEY_MARGIN).append(" * ");
-                    int stop = message.indexOf('\n');
-                    if (stop < 0) {
-                        stop = message.length();
-                    }
-                    if (stop > COMMENT_LENGTH) {
-                        stop = COMMENT_LENGTH;
-                        while (stop>20 && !Character.isWhitespace(message.charAt(stop)))
{
-                            stop--;
+        boolean modified;
+        try (BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file),
JAVA_ENCODING))) {
+            String line;
+            do {
+                line = in.readLine();
+                if (line == null) {
+                    in.close();
+                    throw new EOFException(file.toString());
+                }
+                buffer.append(line).append(lineSeparator);
+            } while (!classKeys.matcher(line).matches());
+            /*
+             * Starting from this point, the content that we are going to write in the buffer
+             * may be different than the file content.  Remember the buffer position in order
+             * to allow us to compare the buffer with the file content.
+             *
+             * We stop reading the file for now. We will continue reading the file after
the
+             * 'for' loop below. Instead, we now write the constructor followed by keys values.
+             */
+            int startLineToCompare = buffer.length();
+            buffer.append(KEY_MARGIN).append("private ").append(KEYS_INNER_CLASS).append("()
{").append(lineSeparator)
+                  .append(KEY_MARGIN).append('}').append(lineSeparator);
+            final Map.Entry<?,?>[] entries = allocatedIDs.entrySet().toArray(new Map.Entry<?,?>[allocatedIDs.size()]);
+            Arrays.sort(entries, this);
+            for (int i=0; i<entries.length; i++) {
+                buffer.append(lineSeparator);
+                final String key = (String) entries[i].getValue();
+                final String ID  = entries[i].getKey().toString();
+                String message = (String) resources.get(key);
+                if (message != null) {
+                    message = message.replace('\t', ' ');
+                    buffer.append(KEY_MARGIN).append("/**").append(lineSeparator);
+                    while (((message=message.trim()).length()) != 0) {
+                        buffer.append(KEY_MARGIN).append(" * ");
+                        int stop = message.indexOf('\n');
+                        if (stop < 0) {
+                            stop = message.length();
+                        }
+                        if (stop > COMMENT_LENGTH) {
+                            stop = COMMENT_LENGTH;
+                            while (stop>20 && !Character.isWhitespace(message.charAt(stop)))
{
+                                stop--;
+                            }
                         }
+                        buffer.append(message.substring(0, stop).trim()).append(lineSeparator);
+                        message = message.substring(stop);
                     }
-                    buffer.append(message.substring(0, stop).trim()).append(lineSeparator);
-                    message = message.substring(stop);
+                    buffer.append(KEY_MARGIN).append(" */").append(lineSeparator);
                 }
-                buffer.append(KEY_MARGIN).append(" */").append(lineSeparator);
+                buffer.append(KEY_MARGIN).append(KEY_MODIFIERS).append(key).append(" = ")
+                        .append(ID).append(';').append(lineSeparator);
             }
-            buffer.append(KEY_MARGIN).append(KEY_MODIFIERS).append(key).append(" = ")
-                    .append(ID).append(';').append(lineSeparator);
-        }
-        /*
-         * At this point, all key values have been written in the buffer. Skip the corresponding
-         * lines from the files without adding them to the buffer.  However we will compare
them
-         * to the buffer content in order to detect if we really need to write the file.
-         *
-         * This operation will stop when we reach the closing bracket. Note that opening
brackets
-         * may exist in the code that we are skipping, so we need to count them.
-         */
-        boolean modified = false;
-        int brackets = 1;
-        do {
-            line = in.readLine();
-            if (line == null) {
-                in.close();
-                throw new EOFException(file.toString());
-            }
-            for (int i=0; i<line.length(); i++) {
-                switch (line.charAt(i)) {
-                    case '{': brackets++; break;
-                    case '}': brackets--; break;
+            /*
+             * At this point, all key values have been written in the buffer. Skip the corresponding
+             * lines from the files without adding them to the buffer.  However we will compare
them
+             * to the buffer content in order to detect if we really need to write the file.
+             *
+             * This operation will stop when we reach the closing bracket. Note that opening
brackets
+             * may exist in the code that we are skipping, so we need to count them.
+             */
+            modified = false;
+            int brackets = 1;
+            do {
+                line = in.readLine();
+                if (line == null) {
+                    in.close();
+                    throw new EOFException(file.toString());
                 }
-            }
-            if (!modified) {
-                final int endOfLine = buffer.indexOf(lineSeparator, startLineToCompare);
-                if (endOfLine >= 0) {
-                    if (buffer.substring(startLineToCompare, endOfLine).equals(line)) {
-                        startLineToCompare = endOfLine + lineSeparator.length();
-                        continue; // Content is equals, do not set the 'modified' flag.
+                for (int i=0; i<line.length(); i++) {
+                    switch (line.charAt(i)) {
+                        case '{': brackets++; break;
+                        case '}': brackets--; break;
                     }
-                } else if (brackets == 0) {
-                    break; // Content finished in same time, do not set the 'modified' flag.
                 }
-                modified = true;
-            }
-        } while (brackets != 0);
-        /*
-         * Only if we detected some changes in the file content, read all remaining parts
of
-         * the file then write the result to disk. Note that this overwite the original file.
-         */
-        if (modified) {
-            buffer.append(line).append(lineSeparator);
-            while ((line = in.readLine()) != null) {
+                if (!modified) {
+                    final int endOfLine = buffer.indexOf(lineSeparator, startLineToCompare);
+                    if (endOfLine >= 0) {
+                        if (buffer.substring(startLineToCompare, endOfLine).equals(line))
{
+                            startLineToCompare = endOfLine + lineSeparator.length();
+                            continue; // Content is equals, do not set the 'modified' flag.
+                        }
+                    } else if (brackets == 0) {
+                        break; // Content finished in same time, do not set the 'modified'
flag.
+                    }
+                    modified = true;
+                }
+            } while (brackets != 0);
+            /*
+             * Only if we detected some changes in the file content, read all remaining parts
of
+             * the file then write the result to disk. Note that this overwite the original
file.
+             */
+            if (modified) {
                 buffer.append(line).append(lineSeparator);
+                while ((line = in.readLine()) != null) {
+                    buffer.append(line).append(lineSeparator);
+                }
             }
         }
-        in.close();
         if (modified) {
-            final Writer out = new OutputStreamWriter(new FileOutputStream(file), JAVA_ENCODING);
-            out.write(buffer.toString());
-            out.close();
+            try (Writer out = new OutputStreamWriter(new FileOutputStream(file), JAVA_ENCODING))
{
+                out.write(buffer.toString());
+            }
         }
     }
 



Mime
View raw message