Browse Source

FastByteArrayOutputStream.read byte-to-int conversion

Issue: SPR-17492
pull/23430/head
Juergen Hoeller 6 years ago
parent
commit
35da9f1ddf
  1. 8
      spring-core/src/main/java/org/springframework/util/FastByteArrayOutputStream.java
  2. 28
      spring-core/src/test/java/org/springframework/util/FastByteArrayOutputStreamTests.java

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -367,7 +367,7 @@ public class FastByteArrayOutputStream extends OutputStream { @@ -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()) {
@ -469,7 +469,7 @@ public class FastByteArrayOutputStream extends OutputStream { @@ -469,7 +469,7 @@ public class FastByteArrayOutputStream extends OutputStream {
/**
* Update the message digest with the remaining bytes in this stream.
* @param messageDigest The message digest to update
* @param messageDigest the message digest to update
*/
@Override
public void updateMessageDigest(MessageDigest messageDigest) {
@ -479,7 +479,7 @@ public class FastByteArrayOutputStream extends OutputStream { @@ -479,7 +479,7 @@ public class FastByteArrayOutputStream extends OutputStream {
/**
* Update the message digest with the next len bytes in this stream.
* Avoids creating new byte arrays and use internal buffers for performance.
* @param messageDigest The message digest to update
* @param messageDigest the message digest to update
* @param len how many bytes to read from this stream and use to update the message digest
*/
@Override

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

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,33 +16,28 @@ @@ -16,33 +16,28 @@
package org.springframework.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* Test suite for {@link FastByteArrayOutputStream}
* Test suite for {@link FastByteArrayOutputStream}.
*
* @author Craig Andrews
*/
public class FastByteArrayOutputStreamTests {
private static final int INITIAL_CAPACITY = 256;
private FastByteArrayOutputStream os;
private byte[] helloBytes;
private final FastByteArrayOutputStream os = new FastByteArrayOutputStream(INITIAL_CAPACITY);;
@Before
public void setUp() throws Exception {
this.os = new FastByteArrayOutputStream(INITIAL_CAPACITY);
this.helloBytes = "Hello World".getBytes("UTF-8");
}
private final byte[] helloBytes = "Hello World".getBytes(StandardCharsets.UTF_8);;
@Test
@ -137,6 +132,15 @@ public class FastByteArrayOutputStreamTests { @@ -137,6 +132,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);

Loading…
Cancel
Save