]>
Commit | Line | Data |
---|---|---|
f67539c2 TL |
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 | package org.apache.thrift.transport; | |
21 | ||
22 | import org.apache.thrift.transport.*; | |
23 | import org.apache.thrift.helper.*; | |
24 | ||
25 | import haxe.io.Bytes; | |
26 | import haxe.io.BytesBuffer; | |
27 | import haxe.io.BytesOutput; | |
28 | import haxe.io.BytesInput; | |
29 | ||
30 | ||
31 | class TStreamTransport extends TTransport { | |
32 | ||
33 | public var InputStream(default,null) : TStream; | |
34 | public var OutputStream(default,null) : TStream; | |
35 | ||
36 | ||
37 | public function new( input : TStream, output : TStream) { | |
38 | this.InputStream = input; | |
39 | this.OutputStream = output; | |
40 | } | |
41 | ||
42 | public override function isOpen() : Bool { | |
43 | return true; | |
44 | } | |
45 | ||
46 | public override function peek() : Bool { | |
47 | return (InputStream != null); | |
48 | } | |
49 | ||
50 | public override function open() : Void { | |
51 | } | |
52 | ||
53 | public override function close() : Void { | |
54 | if (InputStream != null) | |
55 | { | |
56 | InputStream.Close(); | |
57 | InputStream = null; | |
58 | } | |
59 | if (OutputStream != null) | |
60 | { | |
61 | OutputStream.Close(); | |
62 | OutputStream = null; | |
63 | } | |
64 | } | |
65 | ||
66 | public override function read( buf : BytesBuffer, off : Int, len : Int) : Int { | |
67 | if (InputStream == null) | |
68 | { | |
69 | throw new TTransportException( TTransportException.NOT_OPEN, | |
70 | "Cannot read from null InputStream"); | |
71 | } | |
72 | ||
73 | var data : Bytes = Bytes.alloc(len); | |
74 | var size = InputStream.Read( data, off, len); | |
75 | buf.addBytes( data, 0, size); | |
76 | return size; | |
77 | } | |
78 | ||
79 | public override function write(buf:Bytes, off : Int, len : Int) : Void { | |
80 | if (OutputStream == null) | |
81 | { | |
82 | throw new TTransportException( TTransportException.NOT_OPEN, | |
83 | "Cannot write to null OutputStream"); | |
84 | } | |
85 | ||
86 | OutputStream.Write(buf, off, len); | |
87 | } | |
88 | ||
89 | public override function flush(callback:Dynamic->Void =null) : Void { | |
90 | if (OutputStream == null) | |
91 | { | |
92 | var err = new TTransportException( TTransportException.NOT_OPEN, | |
93 | "Cannot flush null OutputStream"); | |
94 | if(callback != null) | |
95 | callback(err); | |
96 | else | |
97 | throw err; | |
98 | } | |
99 | ||
100 | OutputStream.Flush(); | |
101 | } | |
102 | ||
103 | } |