Revised ResizableByteArrayOutputStream as an actual subclass of ByteArrayOutputStream, and consistently applied appropriate ByteArrayOutputStream initial capacities across the codebase

Issue: SPR-11594
This commit is contained in:
Juergen Hoeller
2014-03-24 22:57:38 +01:00
parent 05213c684c
commit dd7f54c3c0
23 changed files with 165 additions and 233 deletions

View File

@@ -181,7 +181,7 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
public Object convertToInternal(Object payload, MessageHeaders headers) {
try {
if (byte[].class.equals(getSerializedPayloadClass())) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
JsonEncoding encoding = getJsonEncoding(getMimeType(headers));
// The following has been deprecated as late as Jackson 2.2 (April 2013);

View File

@@ -28,7 +28,6 @@ import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
/**
* Decodes STOMP frames from a {@link ByteBuffer}. If the buffer does not contain
@@ -52,20 +51,15 @@ public class StompDecoder {
* Decodes a STOMP frame in the given {@code buffer} into a {@link Message}.
* If the given ByteBuffer contains partial STOMP frame content, the method
* resets the buffer and returns {@code null}.
*
* @param buffer The buffer to decode the frame from
*
* @return The decoded message or {@code null}
* @param buffer the buffer to decode the frame from
* @return the decoded message or {@code null}
*/
public Message<byte[]> decode(ByteBuffer buffer) {
Message<byte[]> decodedMessage = null;
skipLeadingEol(buffer);
buffer.mark();
String command = readCommand(buffer);
if (command.length() > 0) {
MultiValueMap<String, String> headers = readHeaders(buffer);
byte[] payload = readPayload(buffer, headers);
@@ -108,7 +102,7 @@ public class StompDecoder {
}
private String readCommand(ByteBuffer buffer) {
ByteArrayOutputStream command = new ByteArrayOutputStream();
ByteArrayOutputStream command = new ByteArrayOutputStream(256);
while (buffer.remaining() > 0 && !isEol(buffer)) {
command.write(buffer.get());
}
@@ -118,7 +112,7 @@ public class StompDecoder {
private MultiValueMap<String, String> readHeaders(ByteBuffer buffer) {
MultiValueMap<String, String> headers = new LinkedMultiValueMap<String, String>();
while (true) {
ByteArrayOutputStream headerStream = new ByteArrayOutputStream();
ByteArrayOutputStream headerStream = new ByteArrayOutputStream(256);
while (buffer.remaining() > 0 && !isEol(buffer)) {
headerStream.write(buffer.get());
}
@@ -176,7 +170,7 @@ public class StompDecoder {
}
}
else {
ByteArrayOutputStream payload = new ByteArrayOutputStream();
ByteArrayOutputStream payload = new ByteArrayOutputStream(256);
while (buffer.remaining() > 0) {
byte b = buffer.get();
if (b == 0) {
@@ -208,4 +202,5 @@ public class StompDecoder {
}
return false;
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2014 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.
@@ -26,6 +26,7 @@ import java.util.Map.Entry;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.messaging.Message;
import org.springframework.messaging.simp.SimpMessageType;
@@ -45,23 +46,22 @@ public final class StompEncoder {
private final Log logger = LogFactory.getLog(StompEncoder.class);
/**
* Encodes the given STOMP {@code message} into a {@code byte[]}
*
* @param message The message to encode
*
* @return The encoded message
* @param message the message to encode
* @return the encoded message
*/
public byte[] encode(Message<byte[]> message) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream(256);
DataOutputStream output = new DataOutputStream(baos);
StompHeaderAccessor headers = StompHeaderAccessor.wrap(message);
if (isHeartbeat(headers)) {
output.write(message.getPayload());
} else {
}
else {
writeCommand(headers, output);
writeHeaders(headers, message, output);
output.write(LF);
@@ -77,7 +77,7 @@ public final class StompEncoder {
}
private boolean isHeartbeat(StompHeaderAccessor headers) {
return headers.getMessageType() == SimpMessageType.HEARTBEAT;
return (headers.getMessageType() == SimpMessageType.HEARTBEAT);
}
private void writeCommand(StompHeaderAccessor headers, DataOutputStream output) throws IOException {
@@ -132,4 +132,5 @@ public final class StompEncoder {
.replaceAll("\n", "\\\\n")
.replaceAll("\r", "\\\\r");
}
}
}