1 /*
2  * Created on 17-Jan-2006
3  * Created by Paul Gardner
4  * Copyright (C) Azureus Software, Inc, All Rights Reserved.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  */
19 
20 package com.aelitis.azureus.core.networkmanager.impl;
21 
22 import java.io.IOException;
23 import java.nio.ByteBuffer;
24 
25 
26 public class
27 TransportHelperFilterTransparent
28 	implements TransportHelperFilter
29 {
30 	private TransportHelper		transport;
31 	private boolean				is_plain;
32 
33 	private ByteBuffer			read_insert;
34 
35 	public
TransportHelperFilterTransparent( TransportHelper _transport, boolean _is_plain )36 	TransportHelperFilterTransparent(
37 		TransportHelper		_transport,
38 		boolean				_is_plain )
39 	{
40 		transport	= _transport;
41 		is_plain	= _is_plain;
42 	}
43 
44 	protected void
insertRead( ByteBuffer _read_insert )45 	insertRead(
46 		ByteBuffer	_read_insert )
47 	{
48 		read_insert	= _read_insert;
49 	}
50 
51 	public boolean
hasBufferedWrite()52 	hasBufferedWrite()
53 	{
54 		return( transport.hasDelayedWrite());
55 	}
56 
57 	public boolean
hasBufferedRead()58 	hasBufferedRead()
59 	{
60 		return( read_insert != null && read_insert.remaining() > 0 );
61 	}
62 
63 	public long
write( ByteBuffer[] buffers, int array_offset, int length )64 	write(
65 		ByteBuffer[] 	buffers,
66 		int 			array_offset,
67 		int 			length )
68 
69 		throws IOException
70 	{
71 		return( transport.write( buffers, array_offset, length ));
72 	}
73 
74 	public int
write( ByteBuffer buffer, boolean partial_write )75 	write(
76 		ByteBuffer 		buffer,
77 		boolean			partial_write )
78 
79 		throws IOException
80 	{
81 		return( transport.write( buffer, partial_write ));
82 	}
83 
84 	public long
read( ByteBuffer[] buffers, int array_offset, int length )85 	read(
86 		ByteBuffer[] 	buffers,
87 		int 			array_offset,
88 		int 			length )
89 
90 		throws IOException
91 	{
92 		int	len = 0;
93 
94 		if ( read_insert != null ){
95 
96 			int	pos_before	= read_insert.position();
97 
98 			for (int i=array_offset;i<array_offset+length;i++){
99 
100 				ByteBuffer	buffer = buffers[i];
101 
102 				int	space = buffer.remaining();
103 
104 				if ( space > 0 ){
105 
106 					if ( space < read_insert.remaining()){
107 
108 						int	old_limit = read_insert.limit();
109 
110 						read_insert.limit( read_insert.position() + space );
111 
112 						buffer.put( read_insert );
113 
114 						read_insert.limit( old_limit );
115 
116 					}else{
117 
118 						buffer.put( read_insert );
119 					}
120 
121 					if ( !read_insert.hasRemaining()){
122 
123 						break;
124 					}
125 				}
126 			}
127 
128 			len	= read_insert.position() - pos_before;
129 
130 			if ( read_insert.hasRemaining()){
131 
132 				return( len );
133 
134 			}else{
135 
136 				read_insert	= null;
137 			}
138 		}
139 
140 		return( len + transport.read( buffers, array_offset, length ));
141 	}
142 
143 	public int
read( ByteBuffer buffer )144 	read(
145 		ByteBuffer 		buffer )
146 
147 		throws IOException
148 	{
149 
150 		if ( read_insert != null ){
151 
152 			return((int)read( new ByteBuffer[]{ buffer }, 0, 1 ));
153 		}
154 
155 		return( transport.read( buffer ));
156 	}
157 
158 	public TransportHelper
getHelper()159 	getHelper()
160 	{
161 		return( transport );
162 	}
163 
164 	public void
setTrace( boolean on )165 	setTrace(
166 			boolean	on )
167 	{
168 		transport.setTrace( on );
169 	}
170 
171 	public boolean
isEncrypted()172 	isEncrypted()
173 	{
174 		return( false );
175 	}
176 
177 	public String
getName(boolean verbose)178 	getName(boolean verbose)
179 	{
180 		String proto_str = getHelper().getName(verbose);
181 
182 		if ( proto_str.length() > 0 ){
183 
184 			proto_str = " (" + proto_str + ")";
185 		}
186 
187 		return((is_plain?"Plain":"None") + proto_str );
188 	}
189 }
190