1 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) 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 getContentsAsString(InputStream is, String enc) 71 throws IOException, UnsupportedEncodingException 72 {return new String (getBytes(is), enc);} 73 74 public static String 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 ("You have no default character encoding, and " + 82 "iso-8859-1 is unsupported?!?!"); 83 } 84 } 85 86 public static String getContentsAsString(InputStream is, int max_len, String enc) 87 throws IOException, UnsupportedEncodingException 88 {return new String (getBytes(is, max_len), enc);} 89 90 public static String 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 ("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 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 137 138 private static InputStream EMPTY_ISTREAM = new ByteArrayInputStream(new byte[0]); 139 140 private InputStreamUtils() 141 {} 142 } 143 | Popular Tags |