]>
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.TProcessor; | |
23 | import org.apache.thrift.TException; | |
24 | import org.apache.thrift.protocol.TProtocol; | |
25 | import org.apache.thrift.protocol.TProtocolFactory; | |
26 | ||
27 | /** | |
28 | * FileProcessor: helps in processing files generated by TFileTransport. | |
29 | * Port of original cpp implementation | |
30 | */ | |
31 | public class TFileProcessor { | |
32 | ||
33 | private TProcessor processor_; | |
34 | private TProtocolFactory inputProtocolFactory_; | |
35 | private TProtocolFactory outputProtocolFactory_; | |
36 | private TFileTransport inputTransport_; | |
37 | private TTransport outputTransport_; | |
38 | ||
39 | public TFileProcessor(TProcessor processor, TProtocolFactory protocolFactory, | |
40 | TFileTransport inputTransport, | |
41 | TTransport outputTransport) { | |
42 | processor_ = processor; | |
43 | inputProtocolFactory_ = outputProtocolFactory_ = protocolFactory; | |
44 | inputTransport_ = inputTransport; | |
45 | outputTransport_ = outputTransport; | |
46 | } | |
47 | ||
48 | public TFileProcessor(TProcessor processor, | |
49 | TProtocolFactory inputProtocolFactory, | |
50 | TProtocolFactory outputProtocolFactory, | |
51 | TFileTransport inputTransport, | |
52 | TTransport outputTransport) { | |
53 | processor_ = processor; | |
54 | inputProtocolFactory_ = inputProtocolFactory; | |
55 | outputProtocolFactory_ = outputProtocolFactory; | |
56 | inputTransport_ = inputTransport; | |
57 | outputTransport_ = outputTransport; | |
58 | } | |
59 | ||
60 | private void processUntil(int lastChunk) throws TException { | |
61 | TProtocol ip = inputProtocolFactory_.getProtocol(inputTransport_); | |
62 | TProtocol op = outputProtocolFactory_.getProtocol(outputTransport_); | |
63 | int curChunk = inputTransport_.getCurChunk(); | |
64 | ||
65 | try { | |
66 | while (lastChunk >= curChunk) { | |
67 | processor_.process(ip, op); | |
68 | int newChunk = inputTransport_.getCurChunk(); | |
69 | curChunk = newChunk; | |
70 | } | |
71 | } catch (TTransportException e) { | |
72 | // if we are processing the last chunk - we could have just hit EOF | |
73 | // on EOF - trap the error and stop processing. | |
74 | if(e.getType() != TTransportException.END_OF_FILE) | |
75 | throw e; | |
76 | else { | |
77 | return; | |
78 | } | |
79 | } | |
80 | } | |
81 | ||
82 | /** | |
83 | * Process from start to last chunk both inclusive where chunks begin from 0 | |
84 | ||
85 | * @param startChunkNum first chunk to be processed | |
86 | * @param endChunkNum last chunk to be processed | |
87 | */ | |
88 | public void processChunk(int startChunkNum, int endChunkNum) throws TException { | |
89 | int numChunks = inputTransport_.getNumChunks(); | |
90 | if(endChunkNum < 0) | |
91 | endChunkNum += numChunks; | |
92 | ||
93 | if(startChunkNum < 0) | |
94 | startChunkNum += numChunks; | |
95 | ||
96 | if(endChunkNum < startChunkNum) | |
97 | throw new TException("endChunkNum " + endChunkNum + " is less than " + startChunkNum); | |
98 | ||
99 | inputTransport_.seekToChunk(startChunkNum); | |
100 | processUntil(endChunkNum); | |
101 | } | |
102 | ||
103 | /** | |
104 | * Process a single chunk | |
105 | * | |
106 | * @param chunkNum chunk to be processed | |
107 | */ | |
108 | public void processChunk(int chunkNum) throws TException { | |
109 | processChunk(chunkNum, chunkNum); | |
110 | } | |
111 | ||
112 | /** | |
113 | * Process a current chunk | |
114 | */ | |
115 | public void processChunk() throws TException { | |
116 | processChunk(inputTransport_.getCurChunk()); | |
117 | } | |
118 | } |