1 21 22 package org.apache.derby.iapi.services.io; 23 24 import java.io.Reader ; 25 import java.io.IOException ; 26 27 31 public final class LimitReader extends Reader implements Limit 32 { 33 private int remainingCharacters; 34 private boolean limitInPlace; 35 private Reader reader; 36 37 40 public LimitReader(Reader reader) 41 { 42 super(); 43 this.reader = reader; 44 clearLimit(); 45 } 46 47 public int read() throws IOException 48 { 49 50 if (!limitInPlace) 51 return reader.read(); 52 53 if (remainingCharacters == 0) 54 return -1; 56 57 int value = reader.read(); 58 if (value >= 0) 59 remainingCharacters--; 60 return value; 61 62 } 63 64 public int read(char c[], int off, int len) throws IOException 65 { 66 if (!limitInPlace) 67 return reader.read(c, off, len); 68 69 if (remainingCharacters == 0) 70 return -1; 71 72 if (remainingCharacters < len) 73 { 74 len = remainingCharacters; } 76 77 len = reader.read(c, off, len); 78 if (len >= 0) 79 remainingCharacters -= len; 80 return len; 81 } 82 83 public long skip(long count) 84 throws IOException 85 { 86 if (!limitInPlace) 87 return reader.skip(count); 88 89 if (remainingCharacters == 0) 90 return 0; 92 if (remainingCharacters < count) 93 count = remainingCharacters; 94 95 count = reader.skip(count); 96 remainingCharacters -= count; 97 return count; 98 } 99 100 public void close() 101 throws IOException 102 { 103 reader.close(); 104 } 105 106 117 public void setLimit(int length) 118 { 119 remainingCharacters = length; 120 limitInPlace = true; 121 return; 122 } 123 124 129 public final int getLimit() 130 { 131 return remainingCharacters; 132 } 133 134 141 public int clearLimit() 142 { 143 int leftOver = remainingCharacters; 144 limitInPlace = false; 145 remainingCharacters = -1; 146 return leftOver; 147 } 148 } 149 | Popular Tags |