Skip to content

Commit ed9afa3

Browse files
committed
FastByteArrayOutputStream.read byte-to-int conversion
Issue: SPR-17492
1 parent 22f4b1c commit ed9afa3

File tree

2 files changed

+32
-30
lines changed

2 files changed

+32
-30
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -367,7 +367,7 @@ public int read() {
367367
else {
368368
if (this.nextIndexInCurrentBuffer < this.currentBufferLength) {
369369
this.totalBytesRead++;
370-
return this.currentBuffer[this.nextIndexInCurrentBuffer++];
370+
return this.currentBuffer[this.nextIndexInCurrentBuffer++] & 0xFF;
371371
}
372372
else {
373373
if (this.buffersIterator.hasNext()) {
@@ -487,7 +487,7 @@ public int available() {
487487

488488
/**
489489
* Update the message digest with the remaining bytes in this stream.
490-
* @param messageDigest The message digest to update
490+
* @param messageDigest the message digest to update
491491
*/
492492
@Override
493493
public void updateMessageDigest(MessageDigest messageDigest) {
@@ -497,7 +497,7 @@ public void updateMessageDigest(MessageDigest messageDigest) {
497497
/**
498498
* Update the message digest with the next len bytes in this stream.
499499
* Avoids creating new byte arrays and use internal buffers for performance.
500-
* @param messageDigest The message digest to update
500+
* @param messageDigest the message digest to update
501501
* @param len how many bytes to read from this stream and use to update the message digest
502502
*/
503503
@Override

spring-core/src/test/java/org/springframework/util/FastByteArrayOutputStreamTests.java

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2018 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,39 +16,34 @@
1616

1717
package org.springframework.util;
1818

19+
import java.io.ByteArrayInputStream;
1920
import java.io.ByteArrayOutputStream;
2021
import java.io.IOException;
2122
import java.io.InputStream;
23+
import java.nio.charset.StandardCharsets;
2224

23-
import org.junit.Before;
2425
import org.junit.Test;
2526

2627
import static org.junit.Assert.*;
2728

2829
/**
29-
* Test suite for {@link FastByteArrayOutputStream}
30+
* Test suite for {@link FastByteArrayOutputStream}.
31+
*
3032
* @author Craig Andrews
3133
*/
3234
public class FastByteArrayOutputStreamTests {
3335

3436
private static final int INITIAL_CAPACITY = 256;
3537

36-
private FastByteArrayOutputStream os;
37-
38-
private byte[] helloBytes;
38+
private final FastByteArrayOutputStream os = new FastByteArrayOutputStream(INITIAL_CAPACITY);;
3939

40-
41-
@Before
42-
public void setUp() throws Exception {
43-
this.os = new FastByteArrayOutputStream(INITIAL_CAPACITY);
44-
this.helloBytes = "Hello World".getBytes("UTF-8");
45-
}
40+
private final byte[] helloBytes = "Hello World".getBytes(StandardCharsets.UTF_8);;
4641

4742

4843
@Test
4944
public void size() throws Exception {
5045
this.os.write(this.helloBytes);
51-
assertEquals(this.os.size(), this.helloBytes.length);
46+
assertEquals(this.helloBytes.length, this.os.size());
5247
}
5348

5449
@Test
@@ -124,17 +119,26 @@ public void getInputStream() throws Exception {
124119
@Test
125120
public void getInputStreamAvailable() throws Exception {
126121
this.os.write(this.helloBytes);
127-
assertEquals(this.os.getInputStream().available(), this.helloBytes.length);
122+
assertEquals(this.helloBytes.length, this.os.getInputStream().available());
128123
}
129124

130125
@Test
131126
public void getInputStreamRead() throws Exception {
132127
this.os.write(this.helloBytes);
133128
InputStream inputStream = this.os.getInputStream();
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]);
129+
assertEquals(this.helloBytes[0], inputStream.read());
130+
assertEquals(this.helloBytes[1], inputStream.read());
131+
assertEquals(this.helloBytes[2], inputStream.read());
132+
assertEquals(this.helloBytes[3], inputStream.read());
133+
}
134+
135+
@Test
136+
public void getInputStreamReadBytePromotion() throws Exception {
137+
byte[] bytes = new byte[] { -1 };
138+
this.os.write(bytes);
139+
InputStream inputStream = this.os.getInputStream();
140+
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
141+
assertEquals(bais.read(), inputStream.read());
138142
}
139143

140144
@Test
@@ -166,17 +170,17 @@ public void getInputStreamReadBeyondEndOfStream() throws Exception {
166170
public void getInputStreamSkip() throws Exception {
167171
this.os.write(this.helloBytes);
168172
InputStream inputStream = this.os.getInputStream();
169-
assertEquals(inputStream.read(), this.helloBytes[0]);
170-
assertEquals(inputStream.skip(1), 1);
171-
assertEquals(inputStream.read(), this.helloBytes[2]);
173+
assertEquals(this.helloBytes[0], inputStream.read());
174+
assertEquals(1, inputStream.skip(1));
175+
assertEquals(this.helloBytes[2], inputStream.read());
172176
assertEquals(this.helloBytes.length - 3, inputStream.available());
173177
}
174178

175179
@Test
176180
public void getInputStreamSkipAll() throws Exception {
177181
this.os.write(this.helloBytes);
178182
InputStream inputStream = this.os.getInputStream();
179-
assertEquals(inputStream.skip(1000), this.helloBytes.length);
183+
assertEquals(this.helloBytes.length, inputStream.skip(1000));
180184
assertEquals(0, inputStream.available());
181185
}
182186

@@ -187,8 +191,7 @@ public void updateMessageDigest() throws Exception {
187191
InputStream inputStream = this.os.getInputStream();
188192
DigestUtils.appendMd5DigestAsHex(inputStream, builder);
189193
builder.append("\"");
190-
String actual = builder.toString();
191-
assertEquals("\"0b10a8db164e0754105b7a99be72e3fe5\"", actual);
194+
assertEquals("\"0b10a8db164e0754105b7a99be72e3fe5\"", builder.toString());
192195
}
193196

194197
@Test
@@ -201,8 +204,7 @@ public void updateMessageDigestManyBuffers() throws Exception {
201204
InputStream inputStream = this.os.getInputStream();
202205
DigestUtils.appendMd5DigestAsHex(inputStream, builder);
203206
builder.append("\"");
204-
String actual = builder.toString();
205-
assertEquals("\"06225ca1e4533354c516e74512065331d\"", actual);
207+
assertEquals("\"06225ca1e4533354c516e74512065331d\"", builder.toString());
206208
}
207209

208210

0 commit comments

Comments
 (0)