Subversion Repositories SmartDukaan

Rev

Rev 30 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
30 ashish 1
/**
2
 * Licensed to the Apache Software Foundation (ASF) under one
3
 * or more contributor license agreements. See the NOTICE file
4
 * distributed with this work for additional information
5
 * regarding copyright ownership. The ASF licenses this file
6
 * to you under the Apache License, Version 2.0 (the
7
 * "License"); you may not use this file except in compliance
8
 * with the License. You may obtain a copy of the License at
9
 *
10
 *   http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing,
13
 * software distributed under the License is distributed on an
14
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
 * KIND, either express or implied. See the License for the
16
 * specific language governing permissions and limitations
17
 * under the License.
18
 */
19
 
20
using System;
21
using System.IO;
22
 
23
namespace Thrift.Transport
24
{
25
	public class TBufferedTransport : TTransport
26
	{
27
		private BufferedStream inputBuffer;
28
		private BufferedStream outputBuffer;
29
		private int bufSize;
30
		private TStreamTransport transport;
31
 
32
		public TBufferedTransport(TStreamTransport transport)
33
			:this(transport, 1024)
34
		{
35
 
36
		}
37
 
38
		public TBufferedTransport(TStreamTransport transport, int bufSize)
39
		{
40
			this.bufSize = bufSize;
41
			this.transport = transport;
42
			InitBuffers();
43
		}
44
 
45
		private void InitBuffers()
46
		{
47
			if (transport.InputStream != null)
48
			{
49
				inputBuffer = new BufferedStream(transport.InputStream, bufSize);
50
			}
51
			if (transport.OutputStream != null)
52
			{
53
				outputBuffer = new BufferedStream(transport.OutputStream, bufSize);
54
			}
55
		}
56
 
57
		public TTransport UnderlyingTransport
58
		{
59
			get { return transport; }
60
		}
61
 
62
		public override bool IsOpen
63
		{
64
			get { return transport.IsOpen; }
65
		}
66
 
67
		public override void Open()
68
		{
69
			transport.Open();
70
			InitBuffers();
71
		}
72
 
73
		public override void Close()
74
		{
75
			if (inputBuffer != null && inputBuffer.CanRead)
76
			{
77
				inputBuffer.Close();
78
			}
79
			if (outputBuffer != null && outputBuffer.CanWrite)
80
			{
81
				outputBuffer.Close();
82
			}
83
		}
84
 
85
		public override int Read(byte[] buf, int off, int len)
86
		{
87
			return inputBuffer.Read(buf, off, len);
88
		}
89
 
90
		public override void Write(byte[] buf, int off, int len)
91
		{
92
			outputBuffer.Write(buf, off, len);
93
		}
94
 
95
		public override void Flush()
96
		{
97
			outputBuffer.Flush();
98
		}
99
	}
100
}