From 12f168290d114006b187bf1b1231609c1a5ba7d4 Mon Sep 17 00:00:00 2001 From: Vojtech Janota Date: Tue, 13 Nov 2018 22:04:46 +0100 Subject: [PATCH] SPR-17492: FastByteArrayOutputStream.read byte-to-int conversion --- .../util/FastByteArrayOutputStream.java | 2 +- .../util/FastByteArrayOutputStreamTests.java | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java b/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java index 1eab8fd50f..956e26ebdc 100644 --- a/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java +++ b/spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java @@ -367,7 +367,7 @@ public class FastByteArrayOutputStream extends OutputStream { else { if (this.nextIndexInCurrentBuffer < this.currentBufferLength) { this.totalBytesRead++; - return this.currentBuffer[this.nextIndexInCurrentBuffer++]; + return this.currentBuffer[this.nextIndexInCurrentBuffer++] & 0xFF; } else { if (this.buffersIterator.hasNext()) { diff --git a/spring-core/src/test/java/org/springframework/util/FastByteArrayOutputStreamTests.java b/spring-core/src/test/java/org/springframework/util/FastByteArrayOutputStreamTests.java index 074e80cda7..ae96ba6b6d 100644 --- a/spring-core/src/test/java/org/springframework/util/FastByteArrayOutputStreamTests.java +++ b/spring-core/src/test/java/org/springframework/util/FastByteArrayOutputStreamTests.java @@ -16,6 +16,7 @@ package org.springframework.util; +import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -137,6 +138,15 @@ public class FastByteArrayOutputStreamTests { assertEquals(inputStream.read(), this.helloBytes[3]); } + @Test + public void getInputStreamReadBytePromotion() throws Exception { + byte[] bytes = new byte[] { -1 }; + this.os.write(bytes); + InputStream inputStream = this.os.getInputStream(); + ByteArrayInputStream bais = new ByteArrayInputStream(bytes); + assertEquals(bais.read(), inputStream.read()); + } + @Test public void getInputStreamReadAll() throws Exception { this.os.write(this.helloBytes);