From 62fadaeab1d47e79c9b8033559b89147a4e34398 Mon Sep 17 00:00:00 2001 From: Nikita Date: Fri, 4 Jul 2014 15:23:16 +0400 Subject: [PATCH] b64 encoding optimization --- .../socketio/protocol/PacketEncoder.java | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java b/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java index 195df17..ec9d875 100644 --- a/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java +++ b/src/main/java/com/corundumstudio/socketio/protocol/PacketEncoder.java @@ -54,11 +54,14 @@ public class PacketEncoder { } public void encodeJsonP(Integer jsonpIndex, Queue packets, ByteBuf out, ByteBufAllocator allocator, int limit) throws IOException { - int i = 0; - ByteBuf buf = allocateBuffer(allocator); - boolean jsonpMode = jsonpIndex != null; + ByteBuf buf = out; + if (jsonpMode) { + buf = allocateBuffer(allocator); + } + + int i = 0; while (true) { Packet packet = packets.poll(); if (packet == null || i == limit) { @@ -78,7 +81,7 @@ public class PacketEncoder { buf.writeBytes(toChars(packetSize)); buf.writeBytes(B64_DELIMITER); buf.writeBytes(packetBuf); - + i++; } @@ -92,11 +95,7 @@ public class PacketEncoder { packet = QUOTES_PATTERN.matcher(packet).replaceAll("\\\\\""); packet = new String(packet.getBytes(CharsetUtil.UTF_8), CharsetUtil.ISO_8859_1); out.writeBytes(packet.getBytes(CharsetUtil.UTF_8)); - } else { - out.writeBytes(buf); - } - if (jsonpMode) { out.writeBytes(JSONP_END); } }