|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package guru.nidi.codeassert.util; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStream; |
| 21 | + |
| 22 | +/** |
| 23 | + * A decorating input stream that counts the number of bytes that have passed |
| 24 | + * through the stream so far. |
| 25 | + * <p> |
| 26 | + * A typical use case would be during debugging, to ensure that data is being |
| 27 | + * read as expected. |
| 28 | + */ |
| 29 | +public class CountingInputStream extends ProxyInputStream { |
| 30 | + |
| 31 | + /** |
| 32 | + * The count of bytes that have passed. |
| 33 | + */ |
| 34 | + private long count; |
| 35 | + |
| 36 | + /** |
| 37 | + * Constructs a new CountingInputStream. |
| 38 | + * |
| 39 | + * @param in the InputStream to delegate to |
| 40 | + */ |
| 41 | + public CountingInputStream(final InputStream in) { |
| 42 | + super(in); |
| 43 | + } |
| 44 | + |
| 45 | + //----------------------------------------------------------------------- |
| 46 | + |
| 47 | + /** |
| 48 | + * Skips the stream over the specified number of bytes, adding the skipped |
| 49 | + * amount to the count. |
| 50 | + * |
| 51 | + * @param length the number of bytes to skip |
| 52 | + * @return the actual number of bytes skipped |
| 53 | + * @throws IOException if an I/O error occurs |
| 54 | + * @see java.io.InputStream#skip(long) |
| 55 | + */ |
| 56 | + @Override |
| 57 | + public synchronized long skip(final long length) throws IOException { |
| 58 | + final long skip = super.skip(length); |
| 59 | + this.count += skip; |
| 60 | + return skip; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Adds the number of read bytes to the count. |
| 65 | + * |
| 66 | + * @param n number of bytes read, or -1 if no more bytes are available |
| 67 | + * @since 2.0 |
| 68 | + */ |
| 69 | + @Override |
| 70 | + protected synchronized void afterRead(final int n) { |
| 71 | + if (n != EOF) { |
| 72 | + this.count += n; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + //----------------------------------------------------------------------- |
| 77 | + |
| 78 | + /** |
| 79 | + * The number of bytes that have passed through this stream. |
| 80 | + * <p> |
| 81 | + * NOTE: From v1.3 this method throws an ArithmeticException if the |
| 82 | + * count is greater than can be expressed by an <code>int</code>. |
| 83 | + * See {@link #getByteCount()} for a method using a <code>long</code>. |
| 84 | + * |
| 85 | + * @return the number of bytes accumulated |
| 86 | + * @throws ArithmeticException if the byte count is too large |
| 87 | + */ |
| 88 | + public int getCount() { |
| 89 | + final long result = getByteCount(); |
| 90 | + if (result > Integer.MAX_VALUE) { |
| 91 | + throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); |
| 92 | + } |
| 93 | + return (int) result; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Set the byte count back to 0. |
| 98 | + * <p> |
| 99 | + * NOTE: From v1.3 this method throws an ArithmeticException if the |
| 100 | + * count is greater than can be expressed by an <code>int</code>. |
| 101 | + * See {@link #resetByteCount()} for a method using a <code>long</code>. |
| 102 | + * |
| 103 | + * @return the count previous to resetting |
| 104 | + * @throws ArithmeticException if the byte count is too large |
| 105 | + */ |
| 106 | + public int resetCount() { |
| 107 | + final long result = resetByteCount(); |
| 108 | + if (result > Integer.MAX_VALUE) { |
| 109 | + throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); |
| 110 | + } |
| 111 | + return (int) result; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * The number of bytes that have passed through this stream. |
| 116 | + * <p> |
| 117 | + * NOTE: This method is an alternative for <code>getCount()</code> |
| 118 | + * and was added because that method returns an integer which will |
| 119 | + * result in incorrect count for files over 2GB. |
| 120 | + * |
| 121 | + * @return the number of bytes accumulated |
| 122 | + * @since 1.3 |
| 123 | + */ |
| 124 | + public synchronized long getByteCount() { |
| 125 | + return this.count; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Set the byte count back to 0. |
| 130 | + * <p> |
| 131 | + * NOTE: This method is an alternative for <code>resetCount()</code> |
| 132 | + * and was added because that method returns an integer which will |
| 133 | + * result in incorrect count for files over 2GB. |
| 134 | + * |
| 135 | + * @return the count previous to resetting |
| 136 | + * @since 1.3 |
| 137 | + */ |
| 138 | + public synchronized long resetByteCount() { |
| 139 | + final long tmp = this.count; |
| 140 | + this.count = 0; |
| 141 | + return tmp; |
| 142 | + } |
| 143 | + |
| 144 | +} |
0 commit comments