Skip to content

Commit 12f1682

Browse files
Vojtech Janotajhoeller
Vojtech Janota
authored andcommitted
SPR-17492: FastByteArrayOutputStream.read byte-to-int conversion
1 parent 88cb126 commit 12f1682

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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()) {

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
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;
@@ -137,6 +138,15 @@ public void getInputStreamRead() throws Exception {
137138
assertEquals(inputStream.read(), this.helloBytes[3]);
138139
}
139140

141+
@Test
142+
public void getInputStreamReadBytePromotion() throws Exception {
143+
byte[] bytes = new byte[] { -1 };
144+
this.os.write(bytes);
145+
InputStream inputStream = this.os.getInputStream();
146+
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
147+
assertEquals(bais.read(), inputStream.read());
148+
}
149+
140150
@Test
141151
public void getInputStreamReadAll() throws Exception {
142152
this.os.write(this.helloBytes);

0 commit comments

Comments
 (0)