Skip to content

Commit e03d721

Browse files
author
Stephen Colebourne
committed
IO-84 - Change int methods from deprecated to exception throwng
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/io/trunk@462818 13f79535-47bb-0310-9956-ffa450edef68
1 parent d25ed53 commit e03d721

3 files changed

Lines changed: 56 additions & 35 deletions

File tree

RELEASE-NOTES.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ Bug fixes from 1.2
6363
- Comment about result byte/char count being limited to an int, thus
6464
being inacurate for large streams
6565

66+
- CountingInputStream/CountingOutputStream [IO-84]
67+
- methods were declared as int thus the count was innacurate for large streams
68+
- new long based methods getByteCount()/resetByteCount() added
69+
- existing methods changed to throw an exception if the count is greater than an int
70+
6671

6772
Enhancements from 1.2
6873
---------------------

src/java/org/apache/commons/io/input/CountingInputStream.java

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -110,49 +110,57 @@ public long skip(final long length) throws IOException {
110110
/**
111111
* The number of bytes that have passed through this stream.
112112
* <p>
113-
* <strong>WARNING</strong> This method will return an
114-
* incorrect count for files over 2GB - use
115-
* <code>getByteCount()</code> instead.
113+
* NOTE: From v1.3 this method throws an ArithmeticException if the
114+
* count is greater than can be expressed by an <code>int</code>.
115+
* See {@link #getByteCount()} for a method using a <code>long</code>.
116116
*
117117
* @return the number of bytes accumulated
118-
* @deprecated use <code>getByteCount()</code> - see issue IO-84
118+
* @throws ArithmeticException if the byte count is too large
119119
*/
120-
public int getCount() {
121-
return (int) getByteCount();
120+
public synchronized int getCount() {
121+
long result = getByteCount();
122+
if (result > Integer.MAX_VALUE) {
123+
throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
124+
}
125+
return (int) result;
122126
}
123127

124128
/**
125-
* Set the count back to 0.
129+
* Set the byte count back to 0.
126130
* <p>
127-
* <strong>WARNING</strong> This method will return an
128-
* incorrect count for files over 2GB - use
129-
* <code>resetByteCount()</code> instead.
131+
* NOTE: From v1.3 this method throws an ArithmeticException if the
132+
* count is greater than can be expressed by an <code>int</code>.
133+
* See {@link #resetByteCount()} for a method using a <code>long</code>.
130134
*
131-
* @return the count previous to resetting.
132-
* @deprecated use <code>resetByteCount()</code> - see issue IO-84
135+
* @return the count previous to resetting
136+
* @throws ArithmeticException if the byte count is too large
133137
*/
134138
public synchronized int resetCount() {
135-
return (int) resetByteCount();
139+
long result = resetByteCount();
140+
if (result > Integer.MAX_VALUE) {
141+
throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
142+
}
143+
return (int) result;
136144
}
137145

138146
/**
139147
* The number of bytes that have passed through this stream.
140148
* <p>
141-
* NOTE: This method is a replacement for <code>getCount()</code>
149+
* NOTE: This method is an alternative for <code>getCount()</code>
142150
* and was added because that method returns an integer which will
143151
* result in incorrect count for files over 2GB.
144152
*
145153
* @return the number of bytes accumulated
146154
* @since Commons IO 1.3
147155
*/
148-
public long getByteCount() {
156+
public synchronized long getByteCount() {
149157
return this.count;
150158
}
151159

152160
/**
153161
* Set the byte count back to 0.
154162
* <p>
155-
* NOTE: This method is a replacement for <code>resetCount()</code>
163+
* NOTE: This method is an alternative for <code>resetCount()</code>
156164
* and was added because that method returns an integer which will
157165
* result in incorrect count for files over 2GB.
158166
*

src/java/org/apache/commons/io/output/CountingOutputStream.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,49 +88,57 @@ public void write(int b) throws IOException {
8888
/**
8989
* The number of bytes that have passed through this stream.
9090
* <p>
91-
* <strong>WARNING</strong> This method will return an
92-
* incorrect count for files over 2GB - use
93-
* <code>getByteCount()</code> instead.
91+
* NOTE: From v1.3 this method throws an ArithmeticException if the
92+
* count is greater than can be expressed by an <code>int</code>.
93+
* See {@link #getByteCount()} for a method using a <code>long</code>.
9494
*
9595
* @return the number of bytes accumulated
96-
* @deprecated use <code>getByteCount()</code> - see issue IO-84
96+
* @throws ArithmeticException if the byte count is too large
9797
*/
98-
public int getCount() {
99-
return (int) getByteCount();
98+
public synchronized int getCount() {
99+
long result = getByteCount();
100+
if (result > Integer.MAX_VALUE) {
101+
throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
102+
}
103+
return (int) result;
100104
}
101105

102106
/**
103-
* Set the count back to 0.
104-
* <p>
105-
* <strong>WARNING</strong> This method will return an
106-
* incorrect count for files over 2GB - use
107-
* <code>resetByteCount()</code> instead.
108-
*
109-
* @return the count previous to resetting.
110-
* @deprecated use <code>resetByteCount()</code> - see issue IO-84
111-
*/
107+
* Set the byte count back to 0.
108+
* <p>
109+
* NOTE: From v1.3 this method throws an ArithmeticException if the
110+
* count is greater than can be expressed by an <code>int</code>.
111+
* See {@link #resetByteCount()} for a method using a <code>long</code>.
112+
*
113+
* @return the count previous to resetting
114+
* @throws ArithmeticException if the byte count is too large
115+
*/
112116
public synchronized int resetCount() {
113-
return (int) resetByteCount();
117+
long result = resetByteCount();
118+
if (result > Integer.MAX_VALUE) {
119+
throw new ArithmeticException("The byte count " + result + " is too large to be converted to an int");
120+
}
121+
return (int) result;
114122
}
115123

116124
/**
117125
* The number of bytes that have passed through this stream.
118126
* <p>
119-
* NOTE: This method is a replacement for <code>getCount()</code>.
127+
* NOTE: This method is an alternative for <code>getCount()</code>.
120128
* It was added because that method returns an integer which will
121129
* result in incorrect count for files over 2GB.
122130
*
123131
* @return the number of bytes accumulated
124132
* @since Commons IO 1.3
125133
*/
126-
public long getByteCount() {
134+
public synchronized long getByteCount() {
127135
return this.count;
128136
}
129137

130138
/**
131139
* Set the byte count back to 0.
132140
* <p>
133-
* NOTE: This method is a replacement for <code>resetCount()</code>.
141+
* NOTE: This method is an alternative for <code>resetCount()</code>.
134142
* It was added because that method returns an integer which will
135143
* result in incorrect count for files over 2GB.
136144
*

0 commit comments

Comments
 (0)