--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.iapi.services.io.LimitReader\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.iapi.services.io;\r
+\r
+import java.io.Reader;\r
+import java.io.IOException;\r
+\r
+/**\r
+ A Reader that provides methods to limit the range that\r
+ can be read from the reader.\r
+*/\r
+public final class LimitReader extends Reader implements Limit \r
+{\r
+ private int remainingCharacters;\r
+ private boolean limitInPlace;\r
+ private Reader reader;\r
+\r
+ /**\r
+ Construct a LimitReader and call the clearLimit() method.\r
+ */\r
+ public LimitReader(Reader reader) \r
+ {\r
+ super();\r
+ this.reader = reader;\r
+ clearLimit();\r
+ }\r
+\r
+ public int read() throws IOException \r
+ {\r
+\r
+ if (!limitInPlace)\r
+ return reader.read();\r
+ \r
+ if (remainingCharacters == 0)\r
+ return -1; // end of file\r
+\r
+ \r
+ int value = reader.read();\r
+ if (value >= 0)\r
+ remainingCharacters--;\r
+ return value;\r
+\r
+ }\r
+\r
+ public int read(char c[], int off, int len) throws IOException \r
+ {\r
+ if (!limitInPlace)\r
+ return reader.read(c, off, len);\r
+\r
+ if (remainingCharacters == 0)\r
+ return -1;\r
+\r
+ if (remainingCharacters < len) \r
+ {\r
+ len = remainingCharacters; // end of file\r
+ }\r
+\r
+ len = reader.read(c, off, len);\r
+ if (len >= 0)\r
+ remainingCharacters -= len;\r
+ return len;\r
+ }\r
+\r
+ public long skip(long count)\r
+ throws IOException \r
+ {\r
+ if (!limitInPlace)\r
+ return reader.skip(count);\r
+\r
+ if (remainingCharacters == 0)\r
+ return 0; // end of file\r
+\r
+ if (remainingCharacters < count)\r
+ count = remainingCharacters;\r
+\r
+ count = reader.skip(count);\r
+ remainingCharacters -= count;\r
+ return count;\r
+ }\r
+\r
+ public void close()\r
+ throws IOException \r
+ {\r
+ reader.close();\r
+ }\r
+\r
+ /**\r
+ Set the limit of the stream that can be read. After this\r
+ call up to and including length characters can be read from\r
+ or skipped in the stream.\r
+ Any attempt to read more than length characters will\r
+ result in an EOFException\r
+\r
+ @exception IOException IOException from some underlying stream\r
+ @exception EOFException The set limit would exceed\r
+ the available data in the stream.\r
+ */\r
+ public void setLimit(int length) \r
+ {\r
+ remainingCharacters = length;\r
+ limitInPlace = true;\r
+ return;\r
+ }\r
+ \r
+ /**\r
+ * return limit of the stream that can be read without throwing\r
+ * EOFException\r
+ * @return the remaining characters left to be read from the stream\r
+ */\r
+ public final int getLimit()\r
+ {\r
+ return remainingCharacters;\r
+ }\r
+\r
+ /**\r
+ Clear any limit set by setLimit. After this call no limit checking\r
+ will be made on any read until a setLimit()) call is made.\r
+\r
+ @return the number of bytes within the limit that have not been read.\r
+ -1 if not limit was set.\r
+ */\r
+ public int clearLimit() \r
+ {\r
+ int leftOver = remainingCharacters;\r
+ limitInPlace = false;\r
+ remainingCharacters = -1;\r
+ return leftOver;\r
+ }\r
+}\r