Hide the TooBigObjectException and just throw a TooLongFrameException

This commit is contained in:
Norman Maurer 2012-05-14 20:10:58 +02:00
parent 6be409a4f6
commit 7b7c585cf3
4 changed files with 26 additions and 45 deletions

View File

@ -26,7 +26,10 @@ import org.jboss.marshalling.ByteInput;
*
*/
class LimitingByteInput implements ByteInput {
// Use a static instance here to remove the overhead of fillStacktrace
private static final TooBigObjectException EXCEPTION = new TooBigObjectException();
private final ByteInput input;
private final long limit;
private long read;
@ -56,7 +59,7 @@ class LimitingByteInput implements ByteInput {
read++;
return b;
} else {
throw new TooBigObjectException();
throw EXCEPTION;
}
}
@ -71,7 +74,7 @@ class LimitingByteInput implements ByteInput {
read += i;
return i;
} else {
throw new TooBigObjectException();
throw EXCEPTION;
}
}
@ -82,11 +85,24 @@ class LimitingByteInput implements ByteInput {
read += i;
return i;
} else {
throw new TooBigObjectException();
throw EXCEPTION;
}
}
private int readable(int length) {
return (int) Math.min(length, limit - read);
}
/**
* Exception that will get thrown if the {@link Object} is to big to unmarshall
*
*/
static final class TooBigObjectException extends IOException {
/**
*
*/
private static final long serialVersionUID = 1L;
}
}

View File

@ -26,6 +26,7 @@ import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ExceptionEvent;
import org.jboss.netty.handler.codec.frame.TooLongFrameException;
import org.jboss.netty.handler.codec.replay.ReplayingDecoder;
import org.jboss.netty.handler.codec.replay.VoidEnum;
@ -70,6 +71,8 @@ public class MarshallingDecoder extends ReplayingDecoder<VoidEnum> {
Object obj = unmarshaller.readObject();
unmarshaller.finish();
return obj;
} catch (LimitingByteInput.TooBigObjectException e) {
throw new TooLongFrameException("Object to big to unmarshal");
} finally {
// Call close in a finally block as the ReplayingDecoder will throw an Error if not enough bytes are
// readable. This helps to be sure that we do not leak resource
@ -98,7 +101,7 @@ public class MarshallingDecoder extends ReplayingDecoder<VoidEnum> {
@Override
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception {
if (e.getCause() instanceof TooBigObjectException) {
if (e.getCause() instanceof TooLongFrameException) {
e.getChannel().close();
} else {

View File

@ -1,39 +0,0 @@
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you 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.jboss.netty.handler.codec.marshalling;
import java.io.IOException;
/**
* Will get thrown if you try to unmarshal a Object that is to big
*
*/
public class TooBigObjectException extends IOException {
/**
*
*/
private static final long serialVersionUID = 533756617517705345L;
public TooBigObjectException() {
super();
}
public TooBigObjectException(String message) {
super(message);
}
}

View File

@ -32,6 +32,7 @@ import org.jboss.netty.buffer.ChannelBuffer;
import org.jboss.netty.buffer.ChannelBuffers;
import org.jboss.netty.handler.codec.embedder.CodecEmbedderException;
import org.jboss.netty.handler.codec.embedder.DecoderEmbedder;
import org.jboss.netty.handler.codec.frame.TooLongFrameException;
import org.junit.Test;
public abstract class AbstractMarshallingDecoderTest {
@ -116,7 +117,7 @@ public abstract class AbstractMarshallingDecoderTest {
decoder.offer(ChannelBuffers.wrappedBuffer(testBytes));
fail();
} catch (CodecEmbedderException e) {
assertEquals(TooBigObjectException.class, e.getCause().getClass());
assertEquals(TooLongFrameException.class, e.getCause().getClass());
}