KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v1 > io > InputStreamUtils


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v1.io;
25
26 import java.io.*;
27 import com.mchange.v2.log.*;
28
29 public final class InputStreamUtils
30 {
31     private final static MLogger logger = MLog.getLogger( InputStreamUtils.class );
32
33     public static boolean compare(InputStream is1, InputStream is2, long num_bytes) throws IOException
34     {
35     int b;
36     for (long num_read = 0; num_read < num_bytes; ++num_read)
37         {
38         if ((b = is1.read()) != is2.read())
39             return false;
40         else if (b < 0) //both EOF
41
break;
42         }
43     return true;
44     }
45
46     public static boolean compare(InputStream is1, InputStream is2) throws IOException
47     {
48     int b = 0;
49     while (b >= 0)
50         if ((b = is1.read()) != is2.read())
51         return false;
52     return true;
53     }
54
55   public static byte[] getBytes(InputStream is, int max_len) throws IOException
56     {
57       ByteArrayOutputStream baos = new ByteArrayOutputStream(max_len);
58       for(int i = 0, b = is.read(); b >= 0 && i < max_len; b = is.read(), ++i)
59     baos.write(b);
60       return baos.toByteArray();
61     }
62
63   public static byte[] getBytes(InputStream is) throws IOException
64     {
65       ByteArrayOutputStream baos = new ByteArrayOutputStream();
66       for(int b = is.read(); b >= 0; b = is.read()) baos.write(b);
67       return baos.toByteArray();
68     }
69
70   public static String JavaDoc getContentsAsString(InputStream is, String JavaDoc enc)
71     throws IOException, UnsupportedEncodingException
72     {return new String JavaDoc(getBytes(is), enc);}
73
74   public static String JavaDoc getContentsAsString(InputStream is)
75     throws IOException
76     {
77       try
78     {return getContentsAsString(is, System.getProperty("file.encoding", "8859_1"));}
79       catch (UnsupportedEncodingException e)
80     {
81       throw new InternalError JavaDoc("You have no default character encoding, and " +
82                   "iso-8859-1 is unsupported?!?!");
83     }
84     }
85
86   public static String JavaDoc getContentsAsString(InputStream is, int max_len, String JavaDoc enc)
87     throws IOException, UnsupportedEncodingException
88     {return new String JavaDoc(getBytes(is, max_len), enc);}
89
90   public static String JavaDoc getContentsAsString(InputStream is, int max_len)
91     throws IOException
92     {
93       try
94     {return getContentsAsString(is, max_len, System.getProperty("file.encoding", "8859_1"));}
95       catch (UnsupportedEncodingException e)
96     {
97       throw new InternalError JavaDoc("You have no default character encoding, and " +
98                   "iso-8859-1 is unsupported?!?!");
99     }
100     }
101
102   public static InputStream getEmptyInputStream()
103     {return EMPTY_ISTREAM;}
104
105   public static void attemptClose(InputStream is)
106     {
107     try
108         {if (is != null) is.close();}
109     catch (IOException e)
110         {
111         //e.printStackTrace();
112
if ( logger.isLoggable( MLevel.WARNING ) )
113             logger.log( MLevel.WARNING, "InputStream close FAILED.", e );
114         }
115     }
116
117   public static void skipFully(InputStream is, long num_bytes) throws EOFException, IOException
118     {
119       long num_skipped = 0;
120       while (num_skipped < num_bytes)
121     {
122       long just_skipped = is.skip(num_bytes - num_skipped);
123       if (just_skipped > 0)
124         num_skipped += just_skipped;
125       else
126         {
127           int test_byte = is.read();
128           if (is.read() < 0)
129         throw new EOFException("Skipped only " + num_skipped + " bytes to end of file.");
130           else
131         ++num_skipped;
132         }
133     }
134     }
135
136   /* Is it appropriate to treat this as a constant? Is it */
137   /* in any discernable sense changed by read() operations */
138   private static InputStream EMPTY_ISTREAM = new ByteArrayInputStream(new byte[0]);
139
140   private InputStreamUtils()
141     {}
142 }
143
Popular Tags