1 26 27 package net.sourceforge.groboutils.codecoverage.v2.datastore; 28 29 import java.io.IOException ; 30 import java.io.Reader ; 31 32 39 class ReadUtil 40 { 41 46 public static final String readTo( Reader in, char val ) 47 throws IOException 48 { 49 StringBuffer sb = new StringBuffer (); 50 int c = in.read(); 51 while (c != -1 && (char)c != val) 52 { 53 sb.append( (char)c ); 54 c = in.read(); 55 } 56 if (c == -1) 57 { 58 throw new IOException ( "Expected '"+val+ 59 "', but found end-of-stream." ); 60 } 61 62 return sb.toString(); 63 } 64 65 66 public static final String readCount( Reader in, int count ) 67 throws IOException 68 { 69 char c[] = new char[ count ]; 70 int read = in.read( c, 0, count ); 71 if (read < 0) 72 { 73 throw new IOException ( "Expected to read "+(count)+ 74 " more characters, but encountered end-of-stream." ); 75 } 76 int totRead = read; 77 while (totRead < count) 78 { 79 read = in.read( c, totRead, (count - totRead) ); 80 if (read < 0) 81 { 82 throw new IOException ( "Expected to read "+(count-totRead)+ 83 " more characters, but encountered end-of-stream." ); 84 } 85 totRead += read; 86 } 87 88 return new String ( c ); 89 } 90 91 92 93 public static final int toInt( String s ) 94 throws IOException 95 { 96 if (s == null || s.length() <= 0) 97 { 98 throw new IOException ( 99 "Invalid empty string; expected an integer." ); 100 } 101 try 102 { 103 int i = Integer.parseInt( s ); 104 return i; 105 } 106 catch (NumberFormatException ex) 107 { 108 throw new IOException ( "String '"+s+"' is not an integer." ); 109 } 110 } 111 112 113 114 115 public static final long toLong( String s ) 116 throws IOException 117 { 118 if (s == null || s.length() <= 0) 119 { 120 throw new IOException ( 121 "Invalid empty string; expected a long integer." ); 122 } 123 try 124 { 125 long i = Long.parseLong( s ); 126 return i; 127 } 128 catch (NumberFormatException ex) 129 { 130 throw new IOException ( "String '"+s+"' is not a long integer." ); 131 } 132 } 133 134 } 135 | Popular Tags |