KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > datastore > ReadUtil


1 /*
2  * @(#)ReadUtil.java
3  *
4  * Copyright (C) 2002-2003 Matt Albrecht
5  * groboclown@users.sourceforge.net
6  * http://groboutils.sourceforge.net
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a
9  * copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation
11  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
12  * and/or sell copies of the Software, and to permit persons to whom the
13  * Software is furnished to do so, subject to the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24  * DEALINGS IN THE SOFTWARE.
25  */

26
27 package net.sourceforge.groboutils.codecoverage.v2.datastore;
28
29 import java.io.IOException JavaDoc;
30 import java.io.Reader JavaDoc;
31
32 /**
33  * Helps with standard read methods. Package protected.
34  *
35  * @author Matt Albrecht <a HREF="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
36  * @version $Date: 2004/04/15 05:48:26 $
37  * @since December 15, 2002
38  */

39 class ReadUtil
40 {
41     /**
42      * reads from the stream up to the given character, but not
43      * including it. If an end-of-stream is encountered before the
44      * character, then an exception is thrown.
45      */

46     public static final String JavaDoc readTo( Reader JavaDoc in, char val )
47             throws IOException JavaDoc
48     {
49         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc( "Expected '"+val+
59                 "', but found end-of-stream." );
60         }
61         
62         return sb.toString();
63     }
64     
65     
66     public static final String JavaDoc readCount( Reader JavaDoc in, int count )
67             throws IOException JavaDoc
68     {
69         char c[] = new char[ count ];
70         int read = in.read( c, 0, count );
71         if (read < 0)
72         {
73             throw new IOException JavaDoc( "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 JavaDoc( "Expected to read "+(count-totRead)+
83                     " more characters, but encountered end-of-stream." );
84             }
85             totRead += read;
86         }
87         
88         return new String JavaDoc( c );
89     }
90
91     
92     
93     public static final int toInt( String JavaDoc s )
94             throws IOException JavaDoc
95     {
96         if (s == null || s.length() <= 0)
97         {
98             throw new IOException JavaDoc(
99                 "Invalid empty string; expected an integer." );
100         }
101         try
102         {
103             int i = Integer.parseInt( s );
104             return i;
105         }
106         catch (NumberFormatException JavaDoc ex)
107         {
108             throw new IOException JavaDoc( "String '"+s+"' is not an integer." );
109         }
110     }
111
112     
113     
114     
115     public static final long toLong( String JavaDoc s )
116             throws IOException JavaDoc
117     {
118         if (s == null || s.length() <= 0)
119         {
120             throw new IOException JavaDoc(
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 JavaDoc ex)
129         {
130             throw new IOException JavaDoc( "String '"+s+"' is not a long integer." );
131         }
132     }
133
134 }
135
Popular Tags