From 98e75feeb8578f6404246ec24777044dca07c53f Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Fri, 2 Aug 2013 15:58:40 +0200 Subject: [PATCH] =?UTF-8?q?introduce=20Decoder=20=E2=80=94=20remove=20code?= =?UTF-8?q?=20duplication?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/org/jetbrains/io/Decoder.java | 56 +++++++++++++++++++ .../io/PortUnificationServerHandler.java | 46 +++++++-------- 2 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 platform/platform-impl/src/org/jetbrains/io/Decoder.java diff --git a/platform/platform-impl/src/org/jetbrains/io/Decoder.java b/platform/platform-impl/src/org/jetbrains/io/Decoder.java new file mode 100644 index 000000000000..45464e58860a --- /dev/null +++ b/platform/platform-impl/src/org/jetbrains/io/Decoder.java @@ -0,0 +1,56 @@ +/* + * Copyright 2000-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.jetbrains.io; + +import org.jboss.netty.buffer.ChannelBuffer; +import org.jboss.netty.buffer.ChannelBuffers; +import org.jboss.netty.channel.ChannelHandlerContext; +import org.jboss.netty.channel.SimpleChannelUpstreamHandler; +import org.jetbrains.annotations.Nullable; + +public abstract class Decoder extends SimpleChannelUpstreamHandler { + protected ChannelBuffer cumulation; + + @Nullable + protected final ChannelBuffer getBufferIfSufficient(ChannelBuffer input, int requiredLength, ChannelHandlerContext context) { + if (!input.readable()) { + return null; + } + + if (cumulation == null) { + if (input.readableBytes() < requiredLength) { + cumulation = context.getChannel().getConfig().getBufferFactory().getBuffer(requiredLength); + cumulation.writeBytes(input); + return null; + } + else { + return input; + } + } + else { + if ((cumulation.readableBytes() + input.readableBytes()) < requiredLength) { + cumulation.writeBytes(input); + return null; + } + else { + ChannelBuffer buffer = ChannelBuffers.wrappedBuffer(cumulation, input); + input.skipBytes(input.readableBytes()); + cumulation = null; + return buffer; + } + } + } +} \ No newline at end of file diff --git a/platform/platform-impl/src/org/jetbrains/io/PortUnificationServerHandler.java b/platform/platform-impl/src/org/jetbrains/io/PortUnificationServerHandler.java index 7cfdfa10bf15..1662ab01e16a 100644 --- a/platform/platform-impl/src/org/jetbrains/io/PortUnificationServerHandler.java +++ b/platform/platform-impl/src/org/jetbrains/io/PortUnificationServerHandler.java @@ -1,8 +1,22 @@ +/* + * Copyright 2000-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.jetbrains.io; import com.intellij.openapi.util.AtomicNotNullLazyValue; import org.jboss.netty.buffer.ChannelBuffer; -import org.jboss.netty.buffer.ChannelBuffers; import org.jboss.netty.channel.*; import org.jboss.netty.channel.group.ChannelGroup; import org.jboss.netty.handler.codec.compression.ZlibDecoder; @@ -25,7 +39,7 @@ import java.security.KeyStore; import java.security.Security; @ChannelHandler.Sharable -final class PortUnificationServerHandler extends SimpleChannelUpstreamHandler { +final class PortUnificationServerHandler extends Decoder { private static final AtomicNotNullLazyValue SSL_SERVER_CONTEXT = new AtomicNotNullLazyValue() { @NotNull @Override @@ -58,8 +72,6 @@ final class PortUnificationServerHandler extends SimpleChannelUpstreamHandler { private final ChannelGroup openChannels; private final DelegatingHttpRequestHandler delegatingHttpRequestHandler; - private ChannelBuffer cumulation; - public PortUnificationServerHandler(ChannelGroup openChannels) { this(new DelegatingHttpRequestHandler(), openChannels, true, true); } @@ -86,29 +98,9 @@ final class PortUnificationServerHandler extends SimpleChannelUpstreamHandler { return; } - ChannelBuffer input = (ChannelBuffer)m; - if (!input.readable()) { - return; - } - - if (cumulation == null) { - if (input.readableBytes() < 5) { - cumulation = context.getChannel().getConfig().getBufferFactory().getBuffer(8); - cumulation.writeBytes(input); - } - else { - decode(context, input, e.getRemoteAddress()); - } - } - else { - if ((cumulation.readableBytes() + input.readableBytes()) < 5) { - cumulation.writeBytes(input); - } - else { - ChannelBuffer compositeBuffer = ChannelBuffers.wrappedBuffer(cumulation, input); - cumulation = null; - decode(context, compositeBuffer, e.getRemoteAddress()); - } + ChannelBuffer buffer = getBufferIfSufficient((ChannelBuffer)m, 5, context); + if (buffer != null) { + decode(context, buffer, e.getRemoteAddress()); } }