Skip to content

Commit 9bf5a5c

Browse files
committed
FastByteArrayInputStream returns correct count from read(byte[])
Issue: SPR-14209
1 parent 9ec0873 commit 9bf5a5c

File tree

2 files changed

+54
-36
lines changed

2 files changed

+54
-36
lines changed

spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,8 @@ else if (off < 0) {
421421
System.arraycopy(this.currentBuffer, this.nextIndexInCurrentBuffer, b, off, bytesToCopy);
422422
this.totalBytesRead += bytesToCopy;
423423
this.nextIndexInCurrentBuffer += bytesToCopy;
424-
return (bytesToCopy + read(b, off + bytesToCopy, len - bytesToCopy));
424+
int remaining = read(b, off + bytesToCopy, len - bytesToCopy);
425+
return bytesToCopy + Math.max(remaining, 0);
425426
}
426427
else {
427428
if (this.buffersIterator.hasNext()) {
Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,15 +16,15 @@
1616

1717
package org.springframework.util;
1818

19-
import static org.junit.Assert.*;
20-
2119
import java.io.ByteArrayOutputStream;
2220
import java.io.IOException;
2321
import java.io.InputStream;
2422

2523
import org.junit.Before;
2624
import org.junit.Test;
2725

26+
import static org.junit.Assert.*;
27+
2828
/**
2929
* Test suite for {@link FastByteArrayOutputStream}
3030
* @author Craig Andrews
@@ -37,21 +37,23 @@ public class FastByteArrayOutputStreamTests {
3737

3838
private byte[] helloBytes;
3939

40+
4041
@Before
4142
public void setUp() throws Exception {
4243
this.os = new FastByteArrayOutputStream(INITIAL_CAPACITY);
4344
this.helloBytes = "Hello World".getBytes("UTF-8");
4445
}
4546

47+
4648
@Test
4749
public void size() throws Exception {
48-
this.os.write(helloBytes);
49-
assertEquals(this.os.size(), helloBytes.length);
50+
this.os.write(this.helloBytes);
51+
assertEquals(this.os.size(), this.helloBytes.length);
5052
}
5153

5254
@Test
5355
public void resize() throws Exception {
54-
this.os.write(helloBytes);
56+
this.os.write(this.helloBytes);
5557
int sizeBefore = this.os.size();
5658
this.os.resize(64);
5759
assertByteArrayEqualsString(this.os);
@@ -70,104 +72,118 @@ public void autoGrow() throws IOException {
7072

7173
@Test
7274
public void write() throws Exception {
73-
this.os.write(helloBytes);
75+
this.os.write(this.helloBytes);
7476
assertByteArrayEqualsString(this.os);
7577
}
7678

7779
@Test
7880
public void reset() throws Exception {
79-
this.os.write(helloBytes);
81+
this.os.write(this.helloBytes);
8082
assertByteArrayEqualsString(this.os);
8183
this.os.reset();
8284
assertEquals(0, this.os.size());
83-
this.os.write(helloBytes);
85+
this.os.write(this.helloBytes);
8486
assertByteArrayEqualsString(this.os);
8587
}
8688

8789
@Test(expected = IOException.class)
8890
public void close() throws Exception {
8991
this.os.close();
90-
this.os.write(helloBytes);
92+
this.os.write(this.helloBytes);
9193
}
9294

9395
@Test
9496
public void toByteArrayUnsafe() throws Exception {
95-
this.os.write(helloBytes);
97+
this.os.write(this.helloBytes);
9698
assertByteArrayEqualsString(this.os);
9799
assertSame(this.os.toByteArrayUnsafe(), this.os.toByteArrayUnsafe());
98-
assertArrayEquals(this.os.toByteArray(), helloBytes);
100+
assertArrayEquals(this.os.toByteArray(), this.helloBytes);
99101
}
100102

101103
@Test
102104
public void writeTo() throws Exception {
103-
this.os.write(helloBytes);
105+
this.os.write(this.helloBytes);
104106
assertByteArrayEqualsString(this.os);
105107
ByteArrayOutputStream baos = new ByteArrayOutputStream();
106108
this.os.writeTo(baos);
107-
assertArrayEquals(baos.toByteArray(), helloBytes);
109+
assertArrayEquals(baos.toByteArray(), this.helloBytes);
108110
}
109111

110112
@Test(expected = IllegalArgumentException.class)
111113
public void failResize() throws Exception {
112-
this.os.write(helloBytes);
114+
this.os.write(this.helloBytes);
113115
this.os.resize(5);
114116
}
115117

116118
@Test
117119
public void getInputStream() throws Exception {
118-
this.os.write(helloBytes);
120+
this.os.write(this.helloBytes);
119121
assertNotNull(this.os.getInputStream());
120122
}
121123

122124
@Test
123125
public void getInputStreamAvailable() throws Exception {
124-
this.os.write(helloBytes);
125-
assertEquals(this.os.getInputStream().available(), helloBytes.length);
126+
this.os.write(this.helloBytes);
127+
assertEquals(this.os.getInputStream().available(), this.helloBytes.length);
126128
}
127129

128130
@Test
129131
public void getInputStreamRead() throws Exception {
130-
this.os.write(helloBytes);
132+
this.os.write(this.helloBytes);
131133
InputStream inputStream = this.os.getInputStream();
132-
assertEquals(inputStream.read(), helloBytes[0]);
133-
assertEquals(inputStream.read(), helloBytes[1]);
134-
assertEquals(inputStream.read(), helloBytes[2]);
135-
assertEquals(inputStream.read(), helloBytes[3]);
134+
assertEquals(inputStream.read(), this.helloBytes[0]);
135+
assertEquals(inputStream.read(), this.helloBytes[1]);
136+
assertEquals(inputStream.read(), this.helloBytes[2]);
137+
assertEquals(inputStream.read(), this.helloBytes[3]);
136138
}
137139

138140
@Test
139141
public void getInputStreamReadAll() throws Exception {
140-
this.os.write(helloBytes);
142+
this.os.write(this.helloBytes);
141143
InputStream inputStream = this.os.getInputStream();
142144
byte[] actual = new byte[inputStream.available()];
143145
int bytesRead = inputStream.read(actual);
144-
assertEquals(bytesRead, helloBytes.length);
145-
assertArrayEquals(actual, helloBytes);
146+
assertEquals(this.helloBytes.length, bytesRead);
147+
assertArrayEquals(this.helloBytes, actual);
148+
assertEquals(0, inputStream.available());
149+
}
150+
151+
@Test
152+
public void getInputStreamReadBeyondEndOfStream() throws Exception {
153+
this.os.write(this.helloBytes);
154+
InputStream inputStream = os.getInputStream();
155+
byte[] actual = new byte[inputStream.available() + 1];
156+
int bytesRead = inputStream.read(actual);
157+
assertEquals(this.helloBytes.length, bytesRead);
158+
for (int i = 0; i < bytesRead; i++) {
159+
assertEquals(this.helloBytes[i], actual[i]);
160+
}
161+
assertEquals(0, actual[this.helloBytes.length]);
146162
assertEquals(0, inputStream.available());
147163
}
148164

149165
@Test
150166
public void getInputStreamSkip() throws Exception {
151-
this.os.write(helloBytes);
167+
this.os.write(this.helloBytes);
152168
InputStream inputStream = this.os.getInputStream();
153-
assertEquals(inputStream.read(), helloBytes[0]);
169+
assertEquals(inputStream.read(), this.helloBytes[0]);
154170
assertEquals(inputStream.skip(1), 1);
155-
assertEquals(inputStream.read(), helloBytes[2]);
156-
assertEquals(helloBytes.length - 3, inputStream.available());
171+
assertEquals(inputStream.read(), this.helloBytes[2]);
172+
assertEquals(this.helloBytes.length - 3, inputStream.available());
157173
}
158174

159175
@Test
160176
public void getInputStreamSkipAll() throws Exception {
161-
this.os.write(helloBytes);
177+
this.os.write(this.helloBytes);
162178
InputStream inputStream = this.os.getInputStream();
163-
assertEquals(inputStream.skip(1000), helloBytes.length);
179+
assertEquals(inputStream.skip(1000), this.helloBytes.length);
164180
assertEquals(0, inputStream.available());
165181
}
166182

167183
@Test
168184
public void updateMessageDigest() throws Exception {
169185
StringBuilder builder = new StringBuilder("\"0");
170-
this.os.write(helloBytes);
186+
this.os.write(this.helloBytes);
171187
InputStream inputStream = this.os.getInputStream();
172188
DigestUtils.appendMd5DigestAsHex(inputStream, builder);
173189
builder.append("\"");
@@ -180,7 +196,7 @@ public void updateMessageDigestManyBuffers() throws Exception {
180196
StringBuilder builder = new StringBuilder("\"0");
181197
// filling at least one 256 buffer
182198
for ( int i = 0; i < 30; i++) {
183-
this.os.write(helloBytes);
199+
this.os.write(this.helloBytes);
184200
}
185201
InputStream inputStream = this.os.getInputStream();
186202
DigestUtils.appendMd5DigestAsHex(inputStream, builder);
@@ -189,8 +205,9 @@ public void updateMessageDigestManyBuffers() throws Exception {
189205
assertEquals("\"06225ca1e4533354c516e74512065331d\"", actual);
190206
}
191207

208+
192209
private void assertByteArrayEqualsString(FastByteArrayOutputStream actual) {
193-
assertArrayEquals(helloBytes, actual.toByteArray());
210+
assertArrayEquals(this.helloBytes, actual.toByteArray());
194211
}
195212

196213
}

0 commit comments

Comments
 (0)