001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v1.io;
024:
025: import java.io.*;
026: import com.mchange.v2.log.*;
027:
028: public final class InputStreamUtils {
029: private final static MLogger logger = MLog
030: .getLogger(InputStreamUtils.class);
031:
032: public static boolean compare(InputStream is1, InputStream is2,
033: long num_bytes) throws IOException {
034: int b;
035: for (long num_read = 0; num_read < num_bytes; ++num_read) {
036: if ((b = is1.read()) != is2.read())
037: return false;
038: else if (b < 0) //both EOF
039: break;
040: }
041: return true;
042: }
043:
044: public static boolean compare(InputStream is1, InputStream is2)
045: throws IOException {
046: int b = 0;
047: while (b >= 0)
048: if ((b = is1.read()) != is2.read())
049: return false;
050: return true;
051: }
052:
053: public static byte[] getBytes(InputStream is, int max_len)
054: throws IOException {
055: ByteArrayOutputStream baos = new ByteArrayOutputStream(max_len);
056: for (int i = 0, b = is.read(); b >= 0 && i < max_len; b = is
057: .read(), ++i)
058: baos.write(b);
059: return baos.toByteArray();
060: }
061:
062: public static byte[] getBytes(InputStream is) throws IOException {
063: ByteArrayOutputStream baos = new ByteArrayOutputStream();
064: for (int b = is.read(); b >= 0; b = is.read())
065: baos.write(b);
066: return baos.toByteArray();
067: }
068:
069: public static String getContentsAsString(InputStream is, String enc)
070: throws IOException, UnsupportedEncodingException {
071: return new String(getBytes(is), enc);
072: }
073:
074: public static String getContentsAsString(InputStream is)
075: throws IOException {
076: try {
077: return getContentsAsString(is, System.getProperty(
078: "file.encoding", "8859_1"));
079: } catch (UnsupportedEncodingException e) {
080: throw new InternalError(
081: "You have no default character encoding, and "
082: + "iso-8859-1 is unsupported?!?!");
083: }
084: }
085:
086: public static String getContentsAsString(InputStream is,
087: int max_len, String enc) throws IOException,
088: UnsupportedEncodingException {
089: return new String(getBytes(is, max_len), enc);
090: }
091:
092: public static String getContentsAsString(InputStream is, int max_len)
093: throws IOException {
094: try {
095: return getContentsAsString(is, max_len, System.getProperty(
096: "file.encoding", "8859_1"));
097: } catch (UnsupportedEncodingException e) {
098: throw new InternalError(
099: "You have no default character encoding, and "
100: + "iso-8859-1 is unsupported?!?!");
101: }
102: }
103:
104: public static InputStream getEmptyInputStream() {
105: return EMPTY_ISTREAM;
106: }
107:
108: public static void attemptClose(InputStream is) {
109: try {
110: if (is != null)
111: is.close();
112: } catch (IOException e) {
113: //e.printStackTrace();
114: if (logger.isLoggable(MLevel.WARNING))
115: logger.log(MLevel.WARNING, "InputStream close FAILED.",
116: e);
117: }
118: }
119:
120: public static void skipFully(InputStream is, long num_bytes)
121: throws EOFException, IOException {
122: long num_skipped = 0;
123: while (num_skipped < num_bytes) {
124: long just_skipped = is.skip(num_bytes - num_skipped);
125: if (just_skipped > 0)
126: num_skipped += just_skipped;
127: else {
128: int test_byte = is.read();
129: if (is.read() < 0)
130: throw new EOFException("Skipped only "
131: + num_skipped + " bytes to end of file.");
132: else
133: ++num_skipped;
134: }
135: }
136: }
137:
138: /* Is it appropriate to treat this as a constant? Is it */
139: /* in any discernable sense changed by read() operations */
140: private static InputStream EMPTY_ISTREAM = new ByteArrayInputStream(
141: new byte[0]);
142:
143: private InputStreamUtils() {
144: }
145: }
|