1 16 package org.apache.commons.vfs.util; 17 18 import java.io.BufferedInputStream ; 19 import java.io.IOException ; 20 import java.io.InputStream ; 21 22 27 public class MonitorInputStream 28 extends BufferedInputStream 29 { 30 private boolean finished; 31 private long count; 32 33 public MonitorInputStream(final InputStream in) 34 { 35 super(in); 36 count = 0; 37 } 38 39 42 public int read() throws IOException 43 { 44 if (finished) 45 { 46 return -1; 47 } 48 49 final int ch = super.read(); 50 if (ch != -1) 51 { 52 count++; 53 return ch; 54 } 55 56 close(); 58 return -1; 59 } 60 61 64 public int read(final byte[] buffer, final int offset, final int length) 65 throws IOException 66 { 67 if (finished) 68 { 69 return -1; 70 } 71 72 final int nread = super.read(buffer, offset, length); 73 if (nread != -1) 74 { 75 count += nread; 76 return nread; 77 } 78 79 close(); 81 return -1; 82 } 83 84 88 public void close() throws IOException 89 { 90 if (finished) 91 { 92 return; 93 } 94 95 IOException exc = null; 97 try 98 { 99 super.close(); 100 } 101 catch (final IOException ioe) 102 { 103 exc = ioe; 104 } 105 106 try 108 { 109 onClose(); 110 } 111 catch (final IOException ioe) 112 { 113 exc = ioe; 114 } 115 116 finished = true; 117 if (exc != null) 118 { 119 throw exc; 120 } 121 } 122 123 127 protected void onClose() throws IOException 128 { 129 } 130 131 134 public long getCount() 135 { 136 return count; 137 } 138 } 139 | Popular Tags |