| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| CountingInputStream |
|
| 1.7142857142857142;1.714 |
| 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 org.apache.commons.io.input; | |
| 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 | * @version $Id: CountingInputStream.java 1415850 2012-11-30 20:51:39Z ggregory $ | |
| 30 | */ | |
| 31 | public class CountingInputStream extends ProxyInputStream { | |
| 32 | ||
| 33 | /** The count of bytes that have passed. */ | |
| 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 | 10 | super(in); |
| 43 | 10 | } |
| 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 | 1 | final long skip = super.skip(length); |
| 59 | 1 | this.count += skip; |
| 60 | 1 | 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 | 1048593 | if (n != -1) { |
| 72 | 1048587 | this.count += n; |
| 73 | } | |
| 74 | 1048593 | } |
| 75 | ||
| 76 | //----------------------------------------------------------------------- | |
| 77 | /** | |
| 78 | * The number of bytes that have passed through this stream. | |
| 79 | * <p> | |
| 80 | * NOTE: From v1.3 this method throws an ArithmeticException if the | |
| 81 | * count is greater than can be expressed by an <code>int</code>. | |
| 82 | * See {@link #getByteCount()} for a method using a <code>long</code>. | |
| 83 | * | |
| 84 | * @return the number of bytes accumulated | |
| 85 | * @throws ArithmeticException if the byte count is too large | |
| 86 | */ | |
| 87 | public int getCount() { | |
| 88 | 16 | final long result = getByteCount(); |
| 89 | 16 | if (result > Integer.MAX_VALUE) { |
| 90 | 1 | throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); |
| 91 | } | |
| 92 | 15 | return (int) result; |
| 93 | } | |
| 94 | ||
| 95 | /** | |
| 96 | * Set the byte count back to 0. | |
| 97 | * <p> | |
| 98 | * NOTE: From v1.3 this method throws an ArithmeticException if the | |
| 99 | * count is greater than can be expressed by an <code>int</code>. | |
| 100 | * See {@link #resetByteCount()} for a method using a <code>long</code>. | |
| 101 | * | |
| 102 | * @return the count previous to resetting | |
| 103 | * @throws ArithmeticException if the byte count is too large | |
| 104 | */ | |
| 105 | public int resetCount() { | |
| 106 | 2 | final long result = resetByteCount(); |
| 107 | 2 | if (result > Integer.MAX_VALUE) { |
| 108 | 1 | throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int"); |
| 109 | } | |
| 110 | 1 | return (int) result; |
| 111 | } | |
| 112 | ||
| 113 | /** | |
| 114 | * The number of bytes that have passed through this stream. | |
| 115 | * <p> | |
| 116 | * NOTE: This method is an alternative for <code>getCount()</code> | |
| 117 | * and was added because that method returns an integer which will | |
| 118 | * result in incorrect count for files over 2GB. | |
| 119 | * | |
| 120 | * @return the number of bytes accumulated | |
| 121 | * @since 1.3 | |
| 122 | */ | |
| 123 | public synchronized long getByteCount() { | |
| 124 | 17 | return this.count; |
| 125 | } | |
| 126 | ||
| 127 | /** | |
| 128 | * Set the byte count back to 0. | |
| 129 | * <p> | |
| 130 | * NOTE: This method is an alternative for <code>resetCount()</code> | |
| 131 | * and was added because that method returns an integer which will | |
| 132 | * result in incorrect count for files over 2GB. | |
| 133 | * | |
| 134 | * @return the count previous to resetting | |
| 135 | * @since 1.3 | |
| 136 | */ | |
| 137 | public synchronized long resetByteCount() { | |
| 138 | 3 | final long tmp = this.count; |
| 139 | 3 | this.count = 0; |
| 140 | 3 | return tmp; |
| 141 | } | |
| 142 | ||
| 143 | } |