1 package com.quadcap.io; 2 3 40 41 import java.io.*; 42 43 import com.quadcap.util.Debug; 44 import com.quadcap.util.Util; 45 46 51 52 public class LimitedOutputStream extends FilterOutputStream { 53 int count = 0; 54 int limit; 55 56 public LimitedOutputStream(OutputStream os, int limit) { 57 super(os); 58 this.limit = limit; 59 } 60 61 public void write(int b) throws IOException { 62 if (count + 1 > limit) { 63 throw new LimitExceededException("Stream limit (" + limit + 64 ") exceeded"); 65 } 66 count++; 67 out.write(b); 68 } 69 70 91 public void write(byte b[], int off, int len) throws IOException { 92 if (count + len > limit) { 93 if (limit - count > 0) { 94 out.write(b, off, limit - count); 95 } 96 throw new LimitExceededException("Stream limit (" + limit + 97 ") exceeded"); 98 } 99 count += len; 100 out.write(b, off, len); 101 } 102 103 } 104 | Popular Tags |