1 21 22 package org.apache.derby.iapi.services.io; 23 24 import java.io.OutputStream ; 25 import java.io.IOException ; 26 import java.io.EOFException ; 27 28 32 33 public class CounterOutputStream extends OutputStream implements Limit { 34 35 protected OutputStream out; 36 private int count; 37 private int limit; 38 39 44 public CounterOutputStream() { 45 super(); 46 } 47 48 public void setOutputStream(OutputStream out) { 49 this.out = out; 50 setLimit(-1); 51 } 52 53 57 public int getCount() { 58 return count; 59 } 60 61 72 public void setLimit(int limit) { 73 74 count = 0; 75 76 this.limit = limit; 77 78 return; 79 } 80 81 public int clearLimit() { 82 83 int unused = limit - count; 84 limit = 0; 85 86 return unused; 87 } 88 89 92 93 98 public void write(int b) throws IOException { 99 100 if ((limit >= 0) && ((count + 1) > limit)) { 101 throw new EOFException (); 102 } 103 104 out.write(b); 105 count++; 106 } 107 108 113 public void write(byte b[]) throws IOException { 114 115 if ((limit >= 0) && ((count + b.length) > limit)) { 116 throw new EOFException (); 117 } 118 119 out.write(b); 120 count += b.length; 121 } 122 123 128 public void write(byte b[], int off, int len) throws IOException { 129 130 if ((limit >= 0) && ((count + len) > limit)) { 131 throw new EOFException (); 132 } 133 134 out.write(b, off, len); 135 count += len; 136 } 137 } 138 | Popular Tags |