Changeset 1232

Show
Ignore:
Timestamp:
07/22/08 18:13:26 (6 months ago)
Author:
phoenix
Message:

Updated misc utils

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/dtool.descent.core/src-util/melnorme/miscutil/ArrayUtil.java

    r675 r1232  
    11package melnorme.miscutil; 
    22 
     3import static melnorme.miscutil.Assert.assertTrue; 
     4 
    35import java.lang.reflect.Array; 
     6import java.util.List; 
    47 
    58public class ArrayUtil { 
     
    811     * as the given array. */ 
    912    @SuppressWarnings("unchecked") 
    10     public static <T> T[] copyFrom(T[] array, int length) { 
    11         T[] copy = (T[]) Array.newInstance(array.getClass().getComponentType(), length); 
    12         System.arraycopy(array, 0, copy, 0, Math.min(array.length, length)); 
     13    public static <T> T[] copyFrom(T[] array, int newLength) { 
     14        T[] copy = (T[]) Array.newInstance(array.getClass().getComponentType(), newLength); 
     15        System.arraycopy(array, 0, copy, 0, Math.min(array.length, newLength)); 
    1316        return copy; 
    1417    } 
    1518     
    1619    /** Creates a new array with the given length, and of type char[] */ 
    17     @SuppressWarnings("unchecked") 
     20    public static char[] copyFrom(char[] array, int newLength) { 
     21        char[] copy = (char[]) Array.newInstance(array.getClass().getComponentType(), newLength); 
     22        System.arraycopy(array, 0, copy, 0, Math.min(array.length, newLength)); 
     23        return copy; 
     24    } 
     25     
     26    /** Creates a new array with the given length, and of type char[] */ 
     27    public static byte[] copyFrom(byte[] array, int newLength) { 
     28        byte[] copy = (byte[]) Array.newInstance(array.getClass().getComponentType(), newLength); 
     29        System.arraycopy(array, 0, copy, 0, Math.min(array.length, newLength)); 
     30        return copy; 
     31    } 
     32     
     33    @Deprecated 
    1834    public static char[] createNew(char[] array, int length) { 
    19          
    20         char[] copy = (char[]) Array.newInstance(array.getClass().getComponentType(), length); 
    21         System.arraycopy(array, 0, copy, 0, Math.min(array.length, length)); 
    22         return copy; 
    23     } 
    24  
    25     /** Appends an element to array, creating a new array. */ 
    26     public static <T> T[] append(T[] array, T element) { 
    27         T[] newArray = copyFrom(array, array.length + 1); 
    28         newArray[array.length] = element; 
    29         return newArray; 
    30     } 
    31  
    32     /** Appends two arrays, creating a new array of the same runtime type as original. */ 
    33     public static <T> T[] concat(T[] original, T... second) { 
    34         T[] newArray = copyFrom(original, original.length + second.length); 
    35         System.arraycopy(second, 0, newArray, original.length, second.length); 
    36         return newArray; 
    37     } 
    38      
    39     /** Appends two arrays, creating a new array of given runtime type. */ 
    40     @SuppressWarnings("unchecked") 
    41     public static <T> T[] concat(T[] original, T[] second, Class<?> arClass) { 
    42         int newSize = original.length + second.length; 
    43         T[] newArray = (T[]) Array.newInstance(arClass, newSize); 
    44         System.arraycopy(original, 0, newArray, 0, original.length); 
    45         System.arraycopy(second, 0, newArray, original.length, second.length); 
    46         return newArray; 
    47     } 
    48      
    49      
    50     /** Removes the given array the first element that equals given obj. */ 
    51     public static<T> T[] remove(T[] array, T obj) { 
    52         for (int i = 0; i < array.length; i++) { 
    53             T elem = array[i]; 
    54             if(elem.equals(obj)); 
    55                 return removeAt(array, i); 
    56         } 
    57         return array; 
    58     } 
    59      
    60      
    61     /** Removes the element at index ix from array, creating a new array. */ 
    62     public static <T> T[] removeAt(T[] array, int ix) { 
    63         T[] newArray = copyFrom(array, array.length - 1); 
    64         System.arraycopy(array, 0, newArray, 0, ix); 
    65         System.arraycopy(array, ix + 1, newArray, ix, array.length - ix - 1);    
    66         return newArray; 
    67     } 
    68      
    69     /** Return true if array contains an element equal to obj. */ 
    70     public static <T> boolean contains(T[] array, T obj) { 
    71         for(T elem: array) { 
    72             if(elem.equals(obj)) 
    73                 return true; 
    74         } 
    75         return false; 
    76     } 
     35        return copyFrom(array, length); 
     36    } 
     37 
    7738     
    7839    /** 
     
    152113        return copy; 
    153114    } 
    154  
    155  
    156  
     115     
     116    /** Create an array from the given list, with the given run-time  
     117     * component type. 
     118     * If the list is null, a zero-length array is created. */ 
     119    @SuppressWarnings("unchecked") 
     120    public static <T> T[] createFrom(List<T> list, Class<T> cpType) { 
     121        if(list == null) 
     122            return (T[])Array.newInstance(cpType, 0); 
     123     
     124        return list.toArray((T[])Array.newInstance(cpType, list.size())); 
     125    } 
     126 
     127    /** Creates an array with the same size as the given list. 
     128     * If the list is null, a zero-length array is created. */ 
     129    @SuppressWarnings("unchecked") 
     130    public static <T> T[] newSameSize(List<?> list, Class<T> cpType) { 
     131        if(list == null) 
     132            return (T[])Array.newInstance(cpType, 0); 
     133         
     134        return (T[])Array.newInstance(cpType, list.size()); 
     135    } 
     136 
     137     
     138    /** Appends an element to array, creating a new array. */ 
     139    public static <T> T[] append(T[] array, T element) { 
     140        T[] newArray = copyFrom(array, array.length + 1); 
     141        newArray[array.length] = element; 
     142        return newArray; 
     143    } 
     144 
     145    /** Appends two arrays, creating a new array of the same runtime type as original. */ 
     146    public static <T> T[] concat(T[] original, T... second) { 
     147        T[] newArray = copyFrom(original, original.length + second.length); 
     148        System.arraycopy(second, 0, newArray, original.length, second.length); 
     149        return newArray; 
     150    } 
     151     
     152    /** Appends two arrays, creating a new array of given runtime type. */ 
     153    @SuppressWarnings("unchecked") 
     154    public static <T> T[] concat(T[] original, T[] second, Class<?> arClass) { 
     155        int newSize = original.length + second.length; 
     156        T[] newArray = (T[]) Array.newInstance(arClass, newSize); 
     157        System.arraycopy(original, 0, newArray, 0, original.length); 
     158        System.arraycopy(second, 0, newArray, original.length, second.length); 
     159        return newArray; 
     160    } 
     161     
     162     
     163    /** Removes the given array the first element that equals given obj. */ 
     164    public static<T> T[] remove(T[] array, T obj) { 
     165        for (int i = 0; i < array.length; i++) { 
     166            T elem = array[i]; 
     167            if(elem.equals(obj)); 
     168                return removeAt(array, i); 
     169        } 
     170        return array; 
     171    } 
     172     
     173     
     174    /** Removes the element at index ix from array, creating a new array. */ 
     175    public static <T> T[] removeAt(T[] array, int ix) { 
     176        T[] newArray = copyFrom(array, array.length - 1); 
     177        System.arraycopy(array, 0, newArray, 0, ix); 
     178        System.arraycopy(array, ix + 1, newArray, ix, array.length - ix - 1);    
     179        return newArray; 
     180    } 
     181     
     182 
     183     
     184    /** Return the index of the first occurrence of elem in array, or -1 if no occurrences. */ 
     185    public static <T> int indexOf(T[] array, T elem) { 
     186        for (int i = 0; i < array.length; i++) { 
     187            if(array[i].equals(elem)) 
     188                return i; 
     189        } 
     190        return -1; 
     191    } 
     192     
     193    /** Return true if array contains an element equal to obj. */ 
     194    public static <T> boolean contains(T[] array, T obj) { 
     195        for(T elem: array) { 
     196            if(elem.equals(obj)) 
     197                return true; 
     198        } 
     199        return false; 
     200    } 
     201     
     202    /** Return the index of the first occurrence of elem in array, or -1 if no occurrences. */ 
     203    public static int indexOf(byte[] array, byte elem) { 
     204        for (int i = 0; i < array.length; i++) { 
     205            if(array[i] == elem) 
     206                return i; 
     207        } 
     208        return -1; 
     209    } 
     210 
     211 
     212     
     213    /** Finds the index in the given array of the element that 
     214     * equal given elem. */ 
     215    public static <T> int getIndexOfEquals(T[] arr, T elem) { 
     216        for (int i = 0; i < arr.length; i++) { 
     217            if(arr[i].equals(elem)); 
     218                return i; 
     219        } 
     220        return -1; 
     221    } 
    157222 
    158223} 
  • trunk/dtool.descent.core/src-util/melnorme/miscutil/Assert.java

    r711 r1232  
    33 
    44/** 
    5  * (Based on org.eclipse.core.runtime.Assert) 
     5 * <p> 
     6 * (Somewhat based on org.eclipse.core.runtime.Assert) 
     7 * </p> 
    68 *  
    7  * <code>Assert</code> is useful for for embedding runtime sanity checks 
    8  * in code. The static predicate methods all test a condition and throw som
    9  * type of unchecked exception if the condition does not hold. 
     9 * <code>Assert</code> is useful for for embedding runtime sanity checks in 
     10 * code. The static predicate methods all test a condition and throw some typ
     11 * of unchecked exception if the condition does not hold. 
    1012 * <p> 
    11  * Assertion failure exceptions, like most runtime exceptions, are 
    12  * thrown when something is misbehaving. Assertion failures are invariably 
    13  * unspecified behavior; consequently, clients should never rely on 
    14  * these being thrown (or not thrown). <b>If you find yourself in the 
    15  * position where you need to catch an assertion failure, you have most  
    16  * certainly written your program incorrectly.</b> 
     13 * Assertion failure exceptions, like most runtime exceptions, are thrown when 
     14 * something is misbehaving. Assertion failures are invariably unspecified 
     15 * behavior; consequently, clients should never rely on these being thrown (or 
     16 * not thrown). <b>If you find yourself in the position where you need to catch 
     17 * an assertion failure, you have most certainly written your program 
     18 * incorrectly.</b> 
     19 * </p> 
     20 * <p> 
     21 * For each method in this class, such as 'isTrue', there is an identical method 
     22 * with the 'assert' prefix, (ie, assertTrue). This is a convenience for the use of 
     23 * Java's static imports. 
    1724 * </p> 
    1825 */ 
     26/* This class is not intended to be instantiated. */ 
    1927public abstract class Assert { 
    2028 
    21     /* This class is not intended to be instantiated. */ 
    2229     
    2330    /** <code>AssertionFailedException</code> is a runtime exception thrown by 
     
    2936     * </p> 
    3037     */ 
    31     private static class AssertionFailedException extends RuntimeException { 
     38    @SuppressWarnings("serial") 
     39    private static class AssertionFailedException extends RuntimeException { 
     40        // Note: it is quite useful to place a class creation breakpoint in this class. 
    3241 
    33         private static final long serialVersionUID = 1L; 
    34  
    35         public AssertionFailedException() { } 
     42        public AssertionFailedException() {  
     43        } 
    3644 
    3745        public AssertionFailedException(String detail) { 
    3846            super(detail); 
    3947        } 
     48         
    4049    } 
    41  
    42     /** Asserts that the given object is <code>null</code>. */ 
    43     public static void isNull(Object obj) { 
    44         Assert.isTrue(obj == null); 
    45     } 
    46  
    47     /** Asserts that the given object is not <code>null</code>. If this 
    48      * is not the case, some kind of unchecked exception is thrown. 
    49      * The given message is included in that exception, to aid debugging. 
    50      */ 
    51     public static void isNotNull(Object object, String message) { 
    52         isTrue(!(object == null), message); 
    53     } 
    54      
    55     /** Like {@link #isNotNull(Object, String)} with empty message.  */ 
    56     public static void isNotNull(Object object) { 
    57         isTrue(!(object == null), ""); 
    58     } 
    59  
    60  
     50     
    6151    /** Asserts that the given boolean is <code>true</code>. If this 
    6252     * is not the case, some kind of unchecked exception is thrown. 
     
    6959        if (!expression) { 
    7060            expression = false;  // dummy statement to allow breakpoint placement 
    71             throw new AssertionFailedException("assertion failed: " + message); //$NON-NLS-1$ 
     61            throw new AssertionFailedException("Assertion failed: " + message); //$NON-NLS-1$ 
    7262        } 
    7363        return expression; 
    7464    } 
    7565     
    76     /** Like {@link #isTrue(boolean, String)} with empty message 
    77      */ 
    78     public static boolean isTrue(boolean expression) { 
    79         return isTrue(expression, ""); //$NON-NLS-1$ 
    80     } 
    81  
    82      
    83     /** Causes an inconditional assertion failure, with message msg. 
    84      */ 
    85     public static void fail(String msg) { 
    86         throw new AssertionFailedException("ASSERT FAIL:" + msg); 
    87     } 
    88      
    89     /** Like {@link #fail(String)} with empty message.  
    90      */ 
    91     public static void fail() { 
    92         throw new AssertionFailedException("ASSERT FAIL"); 
    93     } 
    94  
    95     @Deprecated 
    96     public static void failTODO() { 
    97         fail(); 
    98     } 
    99  
    100     /* ============================================================= */ 
    101  
    102     /** Asserts that the given object is <code>null</code>. */ 
    103     public static void assertIsNull(Object obj) { 
    104         Assert.isTrue(obj == null); 
    105     } 
    106  
    107     /** Asserts that the given object is not <code>null</code>. If this 
    108      * is not the case, some kind of unchecked exception is thrown. 
    109      * The given message is included in that exception, to aid debugging. 
    110      */ 
    111     public static void assertNotNull(Object object, String message) { 
    112         Assert.isTrue(!(object == null), message); 
    113     } 
    114      
    115     /** Like {@link #isNotNull(Object, String)} with empty message.  */ 
    116     public static void assertNotNull(Object object) { 
    117         Assert.isTrue(!(object == null), ""); 
    118     } 
    119  
    120  
    12166    /** Asserts that the given boolean is <code>true</code>. If this 
    12267     * is not the case, some kind of unchecked exception is thrown. 
     
    13277    /** Like {@link #isTrue(boolean, String)} with empty message 
    13378     */ 
     79    public static boolean isTrue(boolean expression) { 
     80        return Assert.isTrue(expression, ""); //$NON-NLS-1$ 
     81    } 
     82    /** Like {@link #isTrue(boolean, String)} with empty message */ 
    13483    public static boolean assertTrue(boolean expression) { 
    13584        return Assert.isTrue(expression, ""); //$NON-NLS-1$ 
    13685    } 
     86     
     87     
     88    /** Asserts that the given object is not <code>null</code>. If this 
     89     * is not the case, some kind of unchecked exception is thrown. 
     90     * The given message is included in that exception, to aid debugging. 
     91     */ 
     92    public static void isNotNull(Object object, String message) { 
     93        Assert.isTrue(!(object == null), message); 
     94    } 
     95    /** Asserts that the given object is not <code>null</code>. If this 
     96     * is not the case, some kind of unchecked exception is thrown. 
     97     * The given message is included in that exception, to aid debugging. 
     98     */ 
     99    public static void assertNotNull(Object object, String message) { 
     100        Assert.isTrue(!(object == null), message); 
     101    } 
     102     
     103    /** Like {@link #isNotNull(Object, String)} with empty message.  */ 
     104    public static void isNotNull(Object object) { 
     105        Assert.isTrue(!(object == null), ""); 
     106    } 
     107    /** Like {@link #isNotNull(Object, String)} with empty message.  */ 
     108    public static void assertNotNull(Object object) { 
     109        Assert.isTrue(!(object == null), ""); 
     110    } 
     111     
     112     
     113    /** Asserts that the given object is <code>null</code>. */ 
     114    public static void isNull(Object object) { 
     115        Assert.isTrue(object == null); 
     116    } 
     117    /** Asserts that the given object is <code>null</code>. */ 
     118    public static void assertIsNull(Object object) { 
     119        Assert.isTrue(object == null); 
     120    } 
    137121 
    138122     
    139     /** Causes an inconditional assertion failure, with message msg. 
     123    /** Causes an inconditional assertion failure, with message msg.  
     124     * Never returns. 
    140125     */ 
    141     public static void assertFail(String msg) { 
    142         Assert.fail(msg); 
     126    public static AssertionFailedException fail(String msg) { 
     127        throw new AssertionFailedException("Assert fail: " + msg); 
     128    } 
     129    /** Causes an inconditional assertion failure, with message msg.  
     130     * Never returns. 
     131     */ 
     132    public static AssertionFailedException assertFail(String msg) { 
     133        return Assert.fail(msg); 
    143134    } 
    144135     
    145     /** Like {@link #fail(String)} with empty message.  
    146      */ 
    147     public static void assertFail() { 
    148         Assert.fail(); 
     136     
     137    /** Like {@link #fail(String)} with empty message. */ 
     138    public static AssertionFailedException fail() { 
     139        throw new AssertionFailedException("Assert fail."); 
     140    } 
     141    /** Like {@link #fail(String)} with empty message. */ 
     142    public static AssertionFailedException assertFail() { 
     143        return Assert.fail(); 
    149144    } 
    150145     
     146     
     147    /** Like {@link #fail()}, specifically signals unreachable code */ 
     148    public static AssertionFailedException unreachable() { 
     149        return Assert.fail("Unreachable code."); 
     150    } 
     151    /** Like {@link #fail()}, specifically signals unreachable code */ 
     152    public static AssertionFailedException assertUnreachable() { 
     153        throw Assert.unreachable(); 
     154    } 
     155 
     156     
     157    /** Causes an Assert.failt signaling a feature that is not yet implemented.  
     158     * Uses the Deprecated annotation solely to cause a warning. */ 
     159    @Deprecated 
     160    public static void failTODO() { 
     161        Assert.fail("TODO"); 
     162    } 
     163    /** Causes an Assert.failt signaling a feature that is not yet implemented.  
     164     * Uses the Deprecated annotation solely to cause a warning. */ 
     165    @Deprecated 
     166    public static void assertFailTODO() { 
     167        Assert.fail("TODO"); 
     168    } 
     169 
    151170} 
  • trunk/dtool.descent.core/src-util/melnorme/miscutil/ExceptionAdapter.java

    r646 r1232  
    11package melnorme.miscutil; 
     2 
     3import static melnorme.miscutil.Assert.assertNotNull; 
     4 
     5import java.io.IOException; 
    26 
    37 
    48/** 
    5  * Excepetion adapter to make checked exceptions less annoying.  
     9 * Exception adapter to make checked exceptions less annoying.  
    610 * Based on Bruce Eckel's article: 
    711 * http://www.mindview.net/Etc/Discussions/CheckedExceptions 
     
    1014public class ExceptionAdapter extends RuntimeException { 
    1115 
    12     // The original checked exception 
    13     private Exception originalException; 
    1416    // Number of frames that originalException traveled while checked 
    15     private int checkedLength;  
     17    protected int checkedLength;  
    1618 
    17     public ExceptionAdapter(Exception e) { 
    18         super(e.toString()); 
    19         originalException = e
     19    protected ExceptionAdapter(Exception e) { 
     20        super(e); 
     21        assertNotNull(e)
    2022         
    2123        // Determine checkedLength based on the difference to this stack trace 
     
    3133    } 
    3234 
    33 /*  public ExceptionAdapter(String string) { 
    34        this(new Exception(string)); 
    35     } 
    36 */ 
    37      
    38     protected void printStackTrace(melnorme.miscutil.log.IPrinter pr) { 
    39         synchronized (pr) { 
    40             pr.println(this); 
    41             StackTraceElement[] trace = originalException.getStackTrace(); 
    42             for (int i=0; i < trace.length; i++) { 
    43                 pr.print("\tat " + trace[i]); 
    44                 if(i == checkedLength) 
    45                     pr.print(" [UNCHECKED]"); 
    46                 pr.println(); 
    47            
     35 
     36    protected void printStackTraceAppendable(Appendable pr) { 
     37        synchronized(pr) { 
     38            try { 
     39               pr.append(this.toString()); 
     40                StackTraceElement[] trace = getCause().getStackTrace(); 
     41               for (int i=0; i < trace.length; i++) { 
     42                   pr.append("\tat " + trace[i]); 
     43                    if(i == checkedLength) 
     44                        pr.append(" [UNCHECKED]"); 
     45                   pr.append("\n"); 
     46                } 
     47            } catch (IOException e) { 
     48               melnorme.miscutil.Assert.assertFail(); 
     49           
    4850        } 
    4951    } 
    5052     
    51     public void printStackTrace(java.io.PrintStream ps) { 
    52         printStackTrace(new melnorme.miscutil.log.StreamPrinter(ps)); 
     53    @Override 
     54    public void printStackTrace(java.io.PrintStream ps) {        
     55        printStackTraceAppendable(ps); 
    5356    } 
    5457 
     58    @Override 
    5559    public void printStackTrace(java.io.PrintWriter pw) { 
    56         printStackTrace(new melnorme.miscutil.log.WriterPrinter(pw)); 
     60        printStackTraceAppendable(pw); 
    5761    } 
    5862 
     63    @Override 
     64    public Exception getCause() { 
     65        return (Exception) super.getCause(); 
     66    } 
    5967 
    6068    public void rethrow() throws Exception { 
    61         throw originalException
     69        throw getCause()
    6270    } 
    6371 
     72    @Override 
    6473    public String toString() { 
    65         //String name = getClass().getName(); 
    66         //return name + "\n>> " + getLocalizedMessage(); 
    67         return "[UE] " + getLocalizedMessage(); 
     74        String className = getClass().getSimpleName(); 
     75        return "["+className+"] " + getLocalizedMessage() + "\n"; 
    6876    } 
    6977     
     
    8290    } 
    8391     
    84     /** Same as unchecked() but stands for a TODO call. */ 
    85     @Deprecated public static RuntimeException uncheckedTODO(Throwable e) { 
     92    /** Same as unchecked() but stands for TO DO code.  
     93     * Uses the Deprecated annotation solely to cause a warning. */ 
     94    @Deprecated  
     95    public static RuntimeException uncheckedTODO(Throwable e) { 
    8696        return unchecked(e); 
    8797    } 
  • trunk/dtool.descent.core/src-util/melnorme/miscutil/FileUtil.java

    r711 r1232  
    11package melnorme.miscutil; 
    22 
     3import java.io.BufferedOutputStream; 
    34import java.io.File; 
     5import java.io.FileInputStream; 
     6import java.io.FileNotFoundException; 
     7import java.io.FileOutputStream; 
    48import java.io.FileReader; 
    59import java.io.IOException; 
     
    913/** 
    1014 * Miscelleanous file utilities  
    11  * XXX: what about closing the streams? 
    1215 */ 
    1316public final class FileUtil { 
    1417 
    15     /** Read all chars available in the given File. */ 
     18    /** Read all bytes of the given file. */ 
     19    public static byte[] readBytesFromFile(File file) throws IOException, FileNotFoundException { 
     20        long length = file.length(); 
     21        /* 
     22         * You cannot create an array using a long type. It needs to be an 
     23         * int type. // Before converting to an int type, check to ensure 
     24         * that file is not larger than Integer.MAX_VALUE. 
     25         */ 
     26        if (length > Integer.MAX_VALUE)  
     27            throw new IOException("File is too large"); 
     28         
     29        byte[] bytes; 
     30         
     31        FileInputStream fis = new FileInputStream(file); 
     32        try { 
     33            // Create the char array to hold the data 
     34            bytes = new byte[(int)length]; 
     35 
     36            // Read in the chars 
     37            int offset = 0; 
     38            int numRead = 0; 
     39            while (offset < bytes.length 
     40                    && (numRead = fis.read(bytes, offset, bytes.length-offset)) >= 0) { 
     41                offset += numRead; 
     42            } 
     43 
     44            // Ensure all the bytes have been read in 
     45            if (offset < bytes.length) { 
     46                throw new IOException("Could not completely read file "+file.getName()); 
     47            } 
     48        } finally { 
     49            fis.close(); 
     50        } 
     51     
     52        return bytes; 
     53    } 
     54 
     55     
     56    /** Read all chars of the given file. */ 
    1657    public static char[] readCharsFromFile(File file) throws IOException { 
    1758 
     
    2364         */ 
    2465        if (length > Integer.MAX_VALUE)  
    25             throw new ExceptionAdapter(new IOException("File is too large")); 
     66            throw new IOException("File is too large"); 
    2667         
     68        char[] chars; 
     69 
    2770        FileReader fr = new java.io.FileReader(file); 
     71        try { 
     72            // Create the char array to hold the data 
     73            chars = new char[(int)length]; 
     74 
     75            // Read in the chars 
     76            int offset = 0; 
     77            int numRead = 0; 
     78            while (offset < chars.length 
     79                   && (numRead = fr.read(chars, offset, chars.length-offset)) >= 0) { 
     80                offset += numRead; 
     81            } 
     82 
     83            // Ensure all the bytes have been read in 
     84            if (offset < chars.length) { 
     85                throw new IOException("Could not completely read file "+file.getName()); 
     86            } 
     87        } finally { 
     88            fr.close(); 
     89        } 
    2890     
    29         // Create the char array to hold the data 
    30         char[] chars = new char[(int)length]; 
     91        return chars; 
     92    } 
    3193     
    32         // Read in the chars 
     94    /** Read all chars in the given Reader until the end of the stream. */ 
     95    public static char[] readCharsFromReader(Reader reader) throws IOException { 
     96        char[] buffer = new char[1024]; 
     97 
     98        // Read in the bytes 
     99        char[] chars = new char[0]; 
    33100        int offset = 0; 
     101 
    34102        int numRead = 0; 
    35         while (offset < chars.length 
    36                && (numRead = fr.read(chars, offset, chars.length-offset)) >= 0) { 
    37             offset += numRead; 
     103        while ( (numRead = reader.read(buffer, 0, 1024)) >= 0) { 
     104            chars = ArrayUtil.copyFrom(chars, offset + numRead); 
     105            System.arraycopy(buffer, 0, chars, offset, numRead); 
     106            offset += numRead; 
    38107        } 
     108         
     109        return chars; 
     110    } 
    39111     
    40         // Ensure all the bytes have been read in 
    41         if (offset < chars.length) { 
    42             throw new IOException("Could not completely read file "+file.getName()); 
    43         } 
    44      
    45         fr.close(); 
    46         return chars; 
     112    /** Write the given array of bytes to given file */ 
     113    public static void writeBytesToFile(byte[] bytes, File file) throws FileNotFoundException, 
     114            IOException { 
     115        FileOutputStream fileOS = new FileOutputStream(file); 
     116        BufferedOutputStream fileBOS = new BufferedOutputStream(fileOS); 
     117        try { 
     118            fileBOS.write(bytes); 
     119        } finally { 
     120            fileBOS.close(); 
     121        } 
    47122    } 
    48123 
     
    52127    } 
    53128 
    54  
    55     /** Read all chars available in the given Reader. */ 
    56     public static char[] readCharsFromReader(Reader reader) throws IOException { 
    57         char[] buffer = new char[1024]; 
    58      
    59         // Read in the bytes 
    60         char[] chars = new char[0]; 
    61         int offset = 0; 
    62  
    63         int numRead = 0; 
    64         while ( (numRead = reader.read(buffer, 0, 1024)) >= 0) { 
    65             chars = ArrayUtil.createNew(chars, offset + numRead); 
    66             System.arraycopy(buffer, 0, chars, offset, numRead); 
    67             offset += numRead; 
    68         } 
    69          
    70         return chars; 
    71     } 
    72129     
    73130    /** Read all chars available in the given Reader, returns a String. */ 
  • trunk/dtool.descent.core/src-util/melnorme/miscutil/IteratorUtil.java

    r646 r1232  
    66public class IteratorUtil {  
    77 
    8     public static final Iterator EMPTY_ITERATOR = Collections.EMPTY_LIST.iterator(); 
     8    public static final Iterator<?> EMPTY_ITERATOR = Collections.EMPTY_LIST.iterator(); 
    99     
    1010    @SuppressWarnings("unchecked") 
    1111    public static <T> Iterator<T> getEMPTY_ITERATOR() { 
    12         return EMPTY_ITERATOR; 
     12        return (Iterator<T>) EMPTY_ITERATOR; 
    1313    } 
    1414 
  • trunk/dtool.descent.core/src-util/melnorme/miscutil/StringUtil.java

    r671 r1232  
    8282            return new String[0]; 
    8383        String[] strs = new String[coll.size()]; 
    84         Iterator iter = coll.iterator(); 
     84        Iterator<?> iter = coll.iterator(); 
    8585        for (int i = 0; i < strs.length; i++) { 
    8686            strs[i] = iter.next().toString(); 
     
    9797    } 
    9898 
     99    /** Replace str with strRep in the given strb StringBuilder, if str occurs. 
     100     * Return true if str occurs in strb. */ 
     101    public static boolean replace(StringBuilder strb, String str, String repStr) { 
     102        int ix = strb.indexOf(str); 
     103        if(ix != -1) { 
     104            strb.replace(ix, ix + str.length(), repStr); 
     105            return true; 
     106        } 
     107        return false; 
     108    } 
     109 
    99110 
    100111} 
  • trunk/dtool.descent.core/src-util/melnorme/miscutil/tree/TreeDepthRecon.java

    r653 r1232  
    3939     
    4040     
     41    @Override 
    4142    public boolean enterNode(IElement element) { 
    4243        depth++; 
     
    5657    } 
    5758     
     59    @Override 
    5860    public void leaveNode(IElement element) { 
    5961        depth--; 
  • trunk/dtool.descent.core/src-util/melnorme/miscutil/tree/TreeVisitor.java

    r671 r1232  
    3939    } 
    4040 
    41     /** Same as {@link #acceptChild(CommonASTVisitor, TreeNode) } */ 
     41    /** Same as {@link #acceptChild(Object, IVisitable) } */ 
    4242    public static <T> void acceptChildren(T visitor, IVisitable<T> child) { 
    4343        TreeVisitor.acceptChild(visitor, child);