1 21 22 package org.apache.derbyTesting.unitTests.services; 23 24 import org.apache.derby.iapi.services.sanity.SanityManager; 25 import java.io.DataInputStream ; 26 import java.io.IOException ; 27 28 38 39 public class MarkedLimitInputStream extends org.apache.derby.iapi.services.io.LimitInputStream 40 { 41 protected boolean isLastBlock; 42 protected int blockLength; 43 44 45 public MarkedLimitInputStream(DataInputStream in) 46 throws IOException 47 { 48 super(in); 49 start(); 50 } 51 52 private void start() 53 throws IOException 54 { 55 isLastBlock = ((DataInputStream ) in).readBoolean(); 56 blockLength = ((DataInputStream ) in).readInt(); 57 58 if (SanityManager.DEBUG) 59 { 60 if (!isLastBlock) 61 { 62 SanityManager.ASSERT(blockLength > 0); 63 } 64 else 65 { 66 SanityManager.ASSERT(blockLength >= 0, "blockLength " + blockLength + " is negative"); 67 } 68 } 69 setLimit(blockLength); 70 71 } 72 73 public int read() 74 throws IOException 75 { 76 int i = super.read(); 77 if (i == -1) 78 { 79 if (isLastBlock) 80 { 81 return -1; 82 } 83 else 84 { 85 start(); 86 return this.read(); 87 } 88 } 89 else 90 { 91 return i; 92 } 93 } 94 95 96 public int read(byte b[], int off, int len) 97 throws IOException 98 { 99 if (isLastBlock) 100 { 101 int m = 0; 104 while (m < len) 105 { 106 int count = super.read(b, off + m, len - m); 107 if (count < 0) 108 { 109 break; 110 } 111 m += count; 112 } 113 return m; 114 } 115 116 int n = 0; 119 while (n < len) 120 { 121 int count = super.read(b, off + n, len - n); 122 if (count < 0) 123 { 124 break; 125 } 126 n += count; 127 } 128 129 if (SanityManager.DEBUG) 130 { 131 SanityManager.ASSERT(n <= len); 132 } 133 if (n == len) 134 { 135 return n; 136 } 137 138 start(); 141 if (n < 0) 143 { 144 return this.read(b,off,len); 145 } 146 147 return n + this.read(b, off+n, len-n); 148 } 149 150 151 public long skip(long count) 152 throws IOException 153 { 154 if (isLastBlock) 155 { 156 return super.skip(count); 157 } 158 159 long n = 0; 163 while (n < count) 164 { 165 long c = super.skip(count-n); 166 if (c <= 0) 167 { 168 break; 169 } 170 n += c; 171 } 172 173 if (SanityManager.DEBUG) 174 { 175 SanityManager.ASSERT(n <= count); 176 } 177 if (n == count) 178 { 179 return n; 180 } 181 start(); 184 if (n < 0) 186 { 187 return this.skip(count); 188 } 189 return n + this.skip(count-n); 190 } 191 192 193 } 194 | Popular Tags |