001: /*
002: * @(#)ReadUtil.java
003: *
004: * Copyright (C) 2002-2003 Matt Albrecht
005: * groboclown@users.sourceforge.net
006: * http://groboutils.sourceforge.net
007: *
008: * Permission is hereby granted, free of charge, to any person obtaining a
009: * copy of this software and associated documentation files (the "Software"),
010: * to deal in the Software without restriction, including without limitation
011: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
012: * and/or sell copies of the Software, and to permit persons to whom the
013: * Software is furnished to do so, subject to the following conditions:
014: *
015: * The above copyright notice and this permission notice shall be included in
016: * all copies or substantial portions of the Software.
017: *
018: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
019: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
020: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
021: * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
022: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
023: * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
024: * DEALINGS IN THE SOFTWARE.
025: */
026:
027: package net.sourceforge.groboutils.codecoverage.v2.datastore;
028:
029: import java.io.IOException;
030: import java.io.Reader;
031:
032: /**
033: * Helps with standard read methods. Package protected.
034: *
035: * @author Matt Albrecht <a href="mailto:groboclown@users.sourceforge.net">groboclown@users.sourceforge.net</a>
036: * @version $Date: 2004/04/15 05:48:26 $
037: * @since December 15, 2002
038: */
039: class ReadUtil {
040: /**
041: * reads from the stream up to the given character, but not
042: * including it. If an end-of-stream is encountered before the
043: * character, then an exception is thrown.
044: */
045: public static final String readTo(Reader in, char val)
046: throws IOException {
047: StringBuffer sb = new StringBuffer();
048: int c = in.read();
049: while (c != -1 && (char) c != val) {
050: sb.append((char) c);
051: c = in.read();
052: }
053: if (c == -1) {
054: throw new IOException("Expected '" + val
055: + "', but found end-of-stream.");
056: }
057:
058: return sb.toString();
059: }
060:
061: public static final String readCount(Reader in, int count)
062: throws IOException {
063: char c[] = new char[count];
064: int read = in.read(c, 0, count);
065: if (read < 0) {
066: throw new IOException(
067: "Expected to read "
068: + (count)
069: + " more characters, but encountered end-of-stream.");
070: }
071: int totRead = read;
072: while (totRead < count) {
073: read = in.read(c, totRead, (count - totRead));
074: if (read < 0) {
075: throw new IOException(
076: "Expected to read "
077: + (count - totRead)
078: + " more characters, but encountered end-of-stream.");
079: }
080: totRead += read;
081: }
082:
083: return new String(c);
084: }
085:
086: public static final int toInt(String s) throws IOException {
087: if (s == null || s.length() <= 0) {
088: throw new IOException(
089: "Invalid empty string; expected an integer.");
090: }
091: try {
092: int i = Integer.parseInt(s);
093: return i;
094: } catch (NumberFormatException ex) {
095: throw new IOException("String '" + s
096: + "' is not an integer.");
097: }
098: }
099:
100: public static final long toLong(String s) throws IOException {
101: if (s == null || s.length() <= 0) {
102: throw new IOException(
103: "Invalid empty string; expected a long integer.");
104: }
105: try {
106: long i = Long.parseLong(s);
107: return i;
108: } catch (NumberFormatException ex) {
109: throw new IOException("String '" + s
110: + "' is not a long integer.");
111: }
112: }
113:
114: }
|