begin_unit|revision:0.9.5;language:Java;cregit-version:0.0.1
begin_comment
comment|/*  * Licensed to the Apache Software Foundation (ASF) under one or more  * contributor license agreements.  See the NOTICE file distributed with  * this work for additional information regarding copyright ownership.  * The ASF 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.  */
end_comment

begin_package
DECL|package|org.apache.camel.test.stub.tcp
package|package
name|org
operator|.
name|apache
operator|.
name|camel
operator|.
name|test
operator|.
name|stub
operator|.
name|tcp
package|;
end_package

begin_import
import|import
name|java
operator|.
name|io
operator|.
name|ByteArrayInputStream
import|;
end_import

begin_import
import|import
name|java
operator|.
name|io
operator|.
name|IOException
import|;
end_import

begin_import
import|import
name|java
operator|.
name|io
operator|.
name|InputStream
import|;
end_import

begin_import
import|import
name|java
operator|.
name|net
operator|.
name|SocketException
import|;
end_import

begin_import
import|import
name|java
operator|.
name|net
operator|.
name|SocketTimeoutException
import|;
end_import

begin_import
import|import
name|java
operator|.
name|util
operator|.
name|LinkedList
import|;
end_import

begin_import
import|import
name|java
operator|.
name|util
operator|.
name|Queue
import|;
end_import

begin_import
import|import
name|java
operator|.
name|util
operator|.
name|StringTokenizer
import|;
end_import

begin_import
import|import
name|org
operator|.
name|apache
operator|.
name|camel
operator|.
name|test
operator|.
name|util
operator|.
name|PayloadBuilder
import|;
end_import

begin_class
DECL|class|SocketInputStreamStub
specifier|public
class|class
name|SocketInputStreamStub
extends|extends
name|InputStream
block|{
DECL|field|useSocketExceptionOnNullPacket
specifier|public
name|boolean
name|useSocketExceptionOnNullPacket
init|=
literal|true
decl_stmt|;
DECL|field|packetQueue
specifier|private
name|Queue
argument_list|<
name|Object
argument_list|>
name|packetQueue
init|=
operator|new
name|LinkedList
argument_list|<>
argument_list|()
decl_stmt|;
annotation|@
name|Override
DECL|method|read ()
specifier|public
name|int
name|read
parameter_list|()
throws|throws
name|IOException
block|{
if|if
condition|(
name|packetQueue
operator|.
name|size
argument_list|()
operator|>
literal|0
condition|)
block|{
if|if
condition|(
name|packetQueue
operator|.
name|peek
argument_list|()
operator|==
literal|null
condition|)
block|{
if|if
condition|(
name|useSocketExceptionOnNullPacket
condition|)
block|{
throw|throw
operator|new
name|SocketException
argument_list|(
literal|"Faking Socket read() failure - simulating reset"
argument_list|)
throw|;
block|}
else|else
block|{
throw|throw
operator|new
name|IOException
argument_list|(
literal|"Faking Socket read() failure"
argument_list|)
throw|;
block|}
block|}
name|Object
name|element
init|=
name|packetQueue
operator|.
name|element
argument_list|()
decl_stmt|;
if|if
condition|(
name|element
operator|instanceof
name|ByteArrayInputStream
condition|)
block|{
name|ByteArrayInputStream
name|inputStreamElement
init|=
operator|(
name|ByteArrayInputStream
operator|)
name|element
decl_stmt|;
name|int
name|answer
init|=
name|inputStreamElement
operator|.
name|read
argument_list|()
decl_stmt|;
if|if
condition|(
name|answer
operator|==
operator|-
literal|1
operator|||
name|inputStreamElement
operator|.
name|available
argument_list|()
operator|==
literal|0
condition|)
block|{
name|packetQueue
operator|.
name|remove
argument_list|()
expr_stmt|;
block|}
return|return
name|answer
return|;
block|}
elseif|else
if|if
condition|(
name|element
operator|instanceof
name|IOException
condition|)
block|{
name|packetQueue
operator|.
name|remove
argument_list|()
expr_stmt|;
throw|throw
operator|(
name|IOException
operator|)
name|element
throw|;
block|}
block|}
throw|throw
operator|new
name|SocketTimeoutException
argument_list|(
literal|"Faking Socket read() Timeout"
argument_list|)
throw|;
block|}
annotation|@
name|Override
DECL|method|read (byte[] buffer)
specifier|public
name|int
name|read
parameter_list|(
name|byte
index|[]
name|buffer
parameter_list|)
throws|throws
name|IOException
block|{
if|if
condition|(
name|packetQueue
operator|.
name|size
argument_list|()
operator|>
literal|0
condition|)
block|{
if|if
condition|(
name|packetQueue
operator|.
name|peek
argument_list|()
operator|==
literal|null
condition|)
block|{
if|if
condition|(
name|useSocketExceptionOnNullPacket
condition|)
block|{
throw|throw
operator|new
name|SocketException
argument_list|(
literal|"Faking Socket read(byte[]) failure - simulating reset"
argument_list|)
throw|;
block|}
else|else
block|{
throw|throw
operator|new
name|IOException
argument_list|(
literal|"Faking Socket read(byte[]) failure"
argument_list|)
throw|;
block|}
block|}
name|Object
name|element
init|=
name|packetQueue
operator|.
name|element
argument_list|()
decl_stmt|;
if|if
condition|(
name|element
operator|instanceof
name|ByteArrayInputStream
condition|)
block|{
name|ByteArrayInputStream
name|inputStreamElement
init|=
operator|(
name|ByteArrayInputStream
operator|)
name|element
decl_stmt|;
name|int
name|answer
init|=
name|inputStreamElement
operator|.
name|read
argument_list|(
name|buffer
argument_list|)
decl_stmt|;
if|if
condition|(
name|answer
operator|==
operator|-
literal|1
operator|||
name|inputStreamElement
operator|.
name|available
argument_list|()
operator|==
literal|0
condition|)
block|{
name|packetQueue
operator|.
name|remove
argument_list|()
expr_stmt|;
block|}
return|return
name|answer
return|;
block|}
elseif|else
if|if
condition|(
name|element
operator|instanceof
name|IOException
condition|)
block|{
name|packetQueue
operator|.
name|remove
argument_list|()
expr_stmt|;
throw|throw
operator|(
name|IOException
operator|)
name|element
throw|;
block|}
block|}
throw|throw
operator|new
name|SocketTimeoutException
argument_list|(
literal|"Faking Socket read(byte[]) Timeout"
argument_list|)
throw|;
block|}
annotation|@
name|Override
DECL|method|read (byte[] buffer, int offset, int length)
specifier|public
name|int
name|read
parameter_list|(
name|byte
index|[]
name|buffer
parameter_list|,
name|int
name|offset
parameter_list|,
name|int
name|length
parameter_list|)
throws|throws
name|IOException
block|{
if|if
condition|(
name|packetQueue
operator|.
name|size
argument_list|()
operator|>
literal|0
condition|)
block|{
if|if
condition|(
name|packetQueue
operator|.
name|peek
argument_list|()
operator|==
literal|null
condition|)
block|{
if|if
condition|(
name|useSocketExceptionOnNullPacket
condition|)
block|{
throw|throw
operator|new
name|SocketException
argument_list|(
literal|"Faking Socket read(byte[], int, int) failure - simulating reset"
argument_list|)
throw|;
block|}
else|else
block|{
throw|throw
operator|new
name|IOException
argument_list|(
literal|"Faking Socket read(byte[], int, int) failure"
argument_list|)
throw|;
block|}
block|}
name|Object
name|element
init|=
name|packetQueue
operator|.
name|element
argument_list|()
decl_stmt|;
if|if
condition|(
name|element
operator|instanceof
name|ByteArrayInputStream
condition|)
block|{
name|ByteArrayInputStream
name|inputStreamElement
init|=
operator|(
name|ByteArrayInputStream
operator|)
name|element
decl_stmt|;
name|int
name|answer
init|=
name|inputStreamElement
operator|.
name|read
argument_list|(
name|buffer
argument_list|,
name|offset
argument_list|,
name|length
argument_list|)
decl_stmt|;
if|if
condition|(
name|answer
operator|==
operator|-
literal|1
operator|||
name|inputStreamElement
operator|.
name|available
argument_list|()
operator|==
literal|0
condition|)
block|{
name|packetQueue
operator|.
name|remove
argument_list|()
expr_stmt|;
block|}
return|return
name|answer
return|;
block|}
elseif|else
if|if
condition|(
name|element
operator|instanceof
name|IOException
condition|)
block|{
name|packetQueue
operator|.
name|remove
argument_list|()
expr_stmt|;
throw|throw
operator|(
name|IOException
operator|)
name|element
throw|;
block|}
block|}
throw|throw
operator|new
name|SocketTimeoutException
argument_list|(
literal|"Faking Socket read(byte[], int, int) Timeout"
argument_list|)
throw|;
block|}
annotation|@
name|Override
DECL|method|available ()
specifier|public
name|int
name|available
parameter_list|()
throws|throws
name|IOException
block|{
if|if
condition|(
name|packetQueue
operator|.
name|size
argument_list|()
operator|>
literal|0
condition|)
block|{
name|Object
name|element
init|=
name|packetQueue
operator|.
name|element
argument_list|()
decl_stmt|;
if|if
condition|(
name|element
operator|instanceof
name|ByteArrayInputStream
condition|)
block|{
name|ByteArrayInputStream
name|inputStreamElement
init|=
operator|(
name|ByteArrayInputStream
operator|)
name|element
decl_stmt|;
return|return
name|inputStreamElement
operator|.
name|available
argument_list|()
return|;
block|}
return|return
literal|1
return|;
block|}
return|return
literal|0
return|;
block|}
DECL|method|addPacket (Exception exception)
specifier|public
name|SocketInputStreamStub
name|addPacket
parameter_list|(
name|Exception
name|exception
parameter_list|)
block|{
name|this
operator|.
name|packetQueue
operator|.
name|add
argument_list|(
name|exception
argument_list|)
expr_stmt|;
return|return
name|this
return|;
block|}
DECL|method|addPacket (char... packet)
specifier|public
name|SocketInputStreamStub
name|addPacket
parameter_list|(
name|char
modifier|...
name|packet
parameter_list|)
block|{
name|this
operator|.
name|packetQueue
operator|.
name|add
argument_list|(
operator|new
name|ByteArrayInputStream
argument_list|(
name|PayloadBuilder
operator|.
name|build
argument_list|(
name|packet
argument_list|)
argument_list|)
argument_list|)
expr_stmt|;
return|return
name|this
return|;
block|}
DECL|method|addPacket (byte[] bytes)
specifier|public
name|SocketInputStreamStub
name|addPacket
parameter_list|(
name|byte
index|[]
name|bytes
parameter_list|)
throws|throws
name|IOException
block|{
if|if
condition|(
name|bytes
operator|!=
literal|null
condition|)
block|{
name|this
operator|.
name|packetQueue
operator|.
name|add
argument_list|(
operator|new
name|ByteArrayInputStream
argument_list|(
name|bytes
argument_list|)
argument_list|)
expr_stmt|;
block|}
else|else
block|{
name|this
operator|.
name|packetQueue
operator|.
name|add
argument_list|(
literal|null
argument_list|)
expr_stmt|;
block|}
return|return
name|this
return|;
block|}
DECL|method|addPacket (byte[] bytes, byte[]... byteArrays)
specifier|public
name|SocketInputStreamStub
name|addPacket
parameter_list|(
name|byte
index|[]
name|bytes
parameter_list|,
name|byte
index|[]
modifier|...
name|byteArrays
parameter_list|)
throws|throws
name|IOException
block|{
name|PayloadBuilder
name|builder
init|=
operator|new
name|PayloadBuilder
argument_list|(
name|bytes
argument_list|)
decl_stmt|;
for|for
control|(
name|byte
index|[]
name|additionalBytes
range|:
name|byteArrays
control|)
block|{
name|builder
operator|.
name|append
argument_list|(
name|additionalBytes
argument_list|)
expr_stmt|;
block|}
name|this
operator|.
name|packetQueue
operator|.
name|add
argument_list|(
operator|new
name|ByteArrayInputStream
argument_list|(
name|builder
operator|.
name|build
argument_list|()
argument_list|)
argument_list|)
expr_stmt|;
return|return
name|this
return|;
block|}
DECL|method|addPacket (String... strings)
specifier|public
name|SocketInputStreamStub
name|addPacket
parameter_list|(
name|String
modifier|...
name|strings
parameter_list|)
throws|throws
name|IOException
block|{
name|this
operator|.
name|packetQueue
operator|.
name|add
argument_list|(
operator|new
name|ByteArrayInputStream
argument_list|(
name|PayloadBuilder
operator|.
name|build
argument_list|(
name|strings
argument_list|)
argument_list|)
argument_list|)
expr_stmt|;
return|return
name|this
return|;
block|}
DECL|method|addPackets (String message, char delimiter)
specifier|public
name|SocketInputStreamStub
name|addPackets
parameter_list|(
name|String
name|message
parameter_list|,
name|char
name|delimiter
parameter_list|)
throws|throws
name|IOException
block|{
name|StringTokenizer
name|tokenizer
init|=
operator|new
name|StringTokenizer
argument_list|(
name|message
argument_list|,
name|String
operator|.
name|valueOf
argument_list|(
name|delimiter
argument_list|)
argument_list|,
literal|true
argument_list|)
decl_stmt|;
while|while
condition|(
name|tokenizer
operator|.
name|hasMoreTokens
argument_list|()
condition|)
block|{
name|addPacket
argument_list|(
name|tokenizer
operator|.
name|nextToken
argument_list|()
argument_list|)
expr_stmt|;
block|}
return|return
name|this
return|;
block|}
DECL|method|addPackets (char... packets)
specifier|public
name|SocketInputStreamStub
name|addPackets
parameter_list|(
name|char
modifier|...
name|packets
parameter_list|)
block|{
for|for
control|(
name|char
name|c
range|:
name|packets
control|)
block|{
name|addPacket
argument_list|(
name|c
argument_list|)
expr_stmt|;
block|}
return|return
name|this
return|;
block|}
DECL|method|addPackets (byte[]... packets)
specifier|public
name|SocketInputStreamStub
name|addPackets
parameter_list|(
name|byte
index|[]
modifier|...
name|packets
parameter_list|)
throws|throws
name|IOException
block|{
for|for
control|(
name|byte
index|[]
name|packet
range|:
name|packets
control|)
block|{
name|addPacket
argument_list|(
name|packet
argument_list|)
expr_stmt|;
block|}
return|return
name|this
return|;
block|}
DECL|method|addPackets (byte[] bytes, String s)
specifier|public
name|SocketInputStreamStub
name|addPackets
parameter_list|(
name|byte
index|[]
name|bytes
parameter_list|,
name|String
name|s
parameter_list|)
throws|throws
name|IOException
block|{
return|return
name|addPacket
argument_list|(
name|bytes
argument_list|)
operator|.
name|addPacket
argument_list|(
name|s
argument_list|)
return|;
block|}
block|}
end_class

end_unit

