Changeset 1232
- Timestamp:
- 07/22/08 18:13:26 (6 months ago)
- Files:
-
- trunk/dtool.descent.core/src-util/melnorme/miscutil/ArrayUtil.java (modified) (3 diffs)
- trunk/dtool.descent.core/src-util/melnorme/miscutil/Assert.java (modified) (4 diffs)
- trunk/dtool.descent.core/src-util/melnorme/miscutil/ExceptionAdapter.java (modified) (4 diffs)
- trunk/dtool.descent.core/src-util/melnorme/miscutil/FileUtil.java (modified) (4 diffs)
- trunk/dtool.descent.core/src-util/melnorme/miscutil/IteratorUtil.java (modified) (1 diff)
- trunk/dtool.descent.core/src-util/melnorme/miscutil/StringUtil.java (modified) (2 diffs)
- trunk/dtool.descent.core/src-util/melnorme/miscutil/log/AbstractPrinter.java (deleted)
- trunk/dtool.descent.core/src-util/melnorme/miscutil/log/IPrinter.java (deleted)
- trunk/dtool.descent.core/src-util/melnorme/miscutil/log/Logg.java (deleted)
- trunk/dtool.descent.core/src-util/melnorme/miscutil/log/SimpleLogger.java (added)
- trunk/dtool.descent.core/src-util/melnorme/miscutil/log/StreamPrinter.java (deleted)
- trunk/dtool.descent.core/src-util/melnorme/miscutil/log/WriterPrinter.java (deleted)
- trunk/dtool.descent.core/src-util/melnorme/miscutil/tree/TreeDepthRecon.java (modified) (2 diffs)
- trunk/dtool.descent.core/src-util/melnorme/miscutil/tree/TreeVisitor.java (modified) (1 diff)
- trunk/dtool.descent.core/src-util/readme.txt (copied) (copied from trunk/descent.core/src-util/readme.txt)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/dtool.descent.core/src-util/melnorme/miscutil/ArrayUtil.java
r675 r1232 1 1 package melnorme.miscutil; 2 2 3 import static melnorme.miscutil.Assert.assertTrue; 4 3 5 import java.lang.reflect.Array; 6 import java.util.List; 4 7 5 8 public class ArrayUtil { … … 8 11 * as the given array. */ 9 12 @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)); 13 16 return copy; 14 17 } 15 18 16 19 /** 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 18 34 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 77 38 78 39 /** … … 152 113 return copy; 153 114 } 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 } 157 222 158 223 } trunk/dtool.descent.core/src-util/melnorme/miscutil/Assert.java
r711 r1232 3 3 4 4 /** 5 * (Based on org.eclipse.core.runtime.Assert) 5 * <p> 6 * (Somewhat based on org.eclipse.core.runtime.Assert) 7 * </p> 6 8 * 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 some9 * typeof 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 type 11 * of unchecked exception if the condition does not hold. 10 12 * <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. 17 24 * </p> 18 25 */ 26 /* This class is not intended to be instantiated. */ 19 27 public abstract class Assert { 20 28 21 /* This class is not intended to be instantiated. */22 29 23 30 /** <code>AssertionFailedException</code> is a runtime exception thrown by … … 29 36 * </p> 30 37 */ 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. 32 41 33 private static final long serialVersionUID = 1L; 34 35 public AssertionFailedException() { } 42 public AssertionFailedException() { 43 } 36 44 37 45 public AssertionFailedException(String detail) { 38 46 super(detail); 39 47 } 48 40 49 } 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 61 51 /** Asserts that the given boolean is <code>true</code>. If this 62 52 * is not the case, some kind of unchecked exception is thrown. … … 69 59 if (!expression) { 70 60 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$ 72 62 } 73 63 return expression; 74 64 } 75 65 76 /** Like {@link #isTrue(boolean, String)} with empty message77 */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 @Deprecated96 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 this108 * 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 121 66 /** Asserts that the given boolean is <code>true</code>. If this 122 67 * is not the case, some kind of unchecked exception is thrown. … … 132 77 /** Like {@link #isTrue(boolean, String)} with empty message 133 78 */ 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 */ 134 83 public static boolean assertTrue(boolean expression) { 135 84 return Assert.isTrue(expression, ""); //$NON-NLS-1$ 136 85 } 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 } 137 121 138 122 139 /** Causes an inconditional assertion failure, with message msg. 123 /** Causes an inconditional assertion failure, with message msg. 124 * Never returns. 140 125 */ 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); 143 134 } 144 135 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(); 149 144 } 150 145 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 151 170 } trunk/dtool.descent.core/src-util/melnorme/miscutil/ExceptionAdapter.java
r646 r1232 1 1 package melnorme.miscutil; 2 3 import static melnorme.miscutil.Assert.assertNotNull; 4 5 import java.io.IOException; 2 6 3 7 4 8 /** 5 * Excep etion adapter to make checked exceptions less annoying.9 * Exception adapter to make checked exceptions less annoying. 6 10 * Based on Bruce Eckel's article: 7 11 * http://www.mindview.net/Etc/Discussions/CheckedExceptions … … 10 14 public class ExceptionAdapter extends RuntimeException { 11 15 12 // The original checked exception13 private Exception originalException;14 16 // Number of frames that originalException traveled while checked 15 pr ivateint checkedLength;17 protected int checkedLength; 16 18 17 p ublicExceptionAdapter(Exception e) {18 super(e .toString());19 originalException = e;19 protected ExceptionAdapter(Exception e) { 20 super(e); 21 assertNotNull(e); 20 22 21 23 // Determine checkedLength based on the difference to this stack trace … … 31 33 } 32 34 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 } 48 50 } 49 51 } 50 52 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); 53 56 } 54 57 58 @Override 55 59 public void printStackTrace(java.io.PrintWriter pw) { 56 printStackTrace (new melnorme.miscutil.log.WriterPrinter(pw));60 printStackTraceAppendable(pw); 57 61 } 58 62 63 @Override 64 public Exception getCause() { 65 return (Exception) super.getCause(); 66 } 59 67 60 68 public void rethrow() throws Exception { 61 throw originalException;69 throw getCause(); 62 70 } 63 71 72 @Override 64 73 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"; 68 76 } 69 77 … … 82 90 } 83 91 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) { 86 96 return unchecked(e); 87 97 } trunk/dtool.descent.core/src-util/melnorme/miscutil/FileUtil.java
r711 r1232 1 1 package melnorme.miscutil; 2 2 3 import java.io.BufferedOutputStream; 3 4 import java.io.File; 5 import java.io.FileInputStream; 6 import java.io.FileNotFoundException; 7 import java.io.FileOutputStream; 4 8 import java.io.FileReader; 5 9 import java.io.IOException; … … 9 13 /** 10 14 * Miscelleanous file utilities 11 * XXX: what about closing the streams?12 15 */ 13 16 public final class FileUtil { 14 17 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. */ 16 57 public static char[] readCharsFromFile(File file) throws IOException { 17 58 … … 23 64 */ 24 65 if (length > Integer.MAX_VALUE) 25 throw new ExceptionAdapter(new IOException("File is too large"));66 throw new IOException("File is too large"); 26 67 68 char[] chars; 69 27 70 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 } 28 90 29 // Create the char array to hold the data30 char[] chars = new char[(int)length];91 return chars; 92 } 31 93 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]; 33 100 int offset = 0; 101 34 102 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; 38 107 } 108 109 return chars; 110 } 39 111 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 } 47 122 } 48 123 … … 52 127 } 53 128 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 bytes60 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 }72 129 73 130 /** Read all chars available in the given Reader, returns a String. */ trunk/dtool.descent.core/src-util/melnorme/miscutil/IteratorUtil.java
r646 r1232 6 6 public class IteratorUtil { 7 7 8 public static final Iterator EMPTY_ITERATOR = Collections.EMPTY_LIST.iterator();8 public static final Iterator<?> EMPTY_ITERATOR = Collections.EMPTY_LIST.iterator(); 9 9 10 10 @SuppressWarnings("unchecked") 11 11 public static <T> Iterator<T> getEMPTY_ITERATOR() { 12 return EMPTY_ITERATOR;12 return (Iterator<T>) EMPTY_ITERATOR; 13 13 } 14 14 trunk/dtool.descent.core/src-util/melnorme/miscutil/StringUtil.java
r671 r1232 82 82 return new String[0]; 83 83 String[] strs = new String[coll.size()]; 84 Iterator iter = coll.iterator();84 Iterator<?> iter = coll.iterator(); 85 85 for (int i = 0; i < strs.length; i++) { 86 86 strs[i] = iter.next().toString(); … … 97 97 } 98 98 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 99 110 100 111 } trunk/dtool.descent.core/src-util/melnorme/miscutil/tree/TreeDepthRecon.java
r653 r1232 39 39 40 40 41 @Override 41 42 public boolean enterNode(IElement element) { 42 43 depth++; … … 56 57 } 57 58 59 @Override 58 60 public void leaveNode(IElement element) { 59 61 depth--; trunk/dtool.descent.core/src-util/melnorme/miscutil/tree/TreeVisitor.java
r671 r1232 39 39 } 40 40 41 /** Same as {@link #acceptChild( CommonASTVisitor, TreeNode) } */41 /** Same as {@link #acceptChild(Object, IVisitable) } */ 42 42 public static <T> void acceptChildren(T visitor, IVisitable<T> child) { 43 43 TreeVisitor.acceptChild(visitor, child);
