Skip to content

Commit 35da9f1

Browse files
committed
FastByteArrayOutputStream.read byte-to-int conversion
Issue: SPR-17492
1 parent e6c9796 commit 35da9f1

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
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()) {
@@ -469,7 +469,7 @@ public int available() {
469469

470470
/**
471471
* Update the message digest with the remaining bytes in this stream.
472-
* @param messageDigest The message digest to update
472+
* @param messageDigest the message digest to update
473473
*/
474474
@Override
475475
public void updateMessageDigest(MessageDigest messageDigest) {
@@ -479,7 +479,7 @@ public void updateMessageDigest(MessageDigest messageDigest) {
479479
/**
480480
* Update the message digest with the next len bytes in this stream.
481481
* Avoids creating new byte arrays and use internal buffers for performance.
482-
* @param messageDigest The message digest to update
482+
* @param messageDigest the message digest to update
483483
* @param len how many bytes to read from this stream and use to update the message digest
484484
*/
485485
@Override

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

Lines changed: 16 additions & 12 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,33 +16,28 @@
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
@@ -137,6 +132,15 @@ public void getInputStreamRead() throws Exception {
137132
assertEquals(inputStream.read(), this.helloBytes[3]);
138133
}
139134

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());
142+
}
143+
140144
@Test
141145
public void getInputStreamReadAll() throws Exception {
142146
this.os.write(this.helloBytes);

0 commit comments

Comments
 (0)