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
10 * http://www.apache.org/licenses/LICENSE-2.0
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
20 package org
.apache
.thrift
.transport
;
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
;
28 * FileProcessor: helps in processing files generated by TFileTransport.
29 * Port of original cpp implementation
31 public class TFileProcessor
{
33 private TProcessor processor_
;
34 private TProtocolFactory inputProtocolFactory_
;
35 private TProtocolFactory outputProtocolFactory_
;
36 private TFileTransport inputTransport_
;
37 private TTransport outputTransport_
;
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
;
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
;
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();
66 while (lastChunk
>= curChunk
) {
67 processor_
.process(ip
, op
);
68 int newChunk
= inputTransport_
.getCurChunk();
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
)
83 * Process from start to last chunk both inclusive where chunks begin from 0
85 * @param startChunkNum first chunk to be processed
86 * @param endChunkNum last chunk to be processed
88 public void processChunk(int startChunkNum
, int endChunkNum
) throws TException
{
89 int numChunks
= inputTransport_
.getNumChunks();
91 endChunkNum
+= numChunks
;
94 startChunkNum
+= numChunks
;
96 if(endChunkNum
< startChunkNum
)
97 throw new TException("endChunkNum " + endChunkNum
+ " is less than " + startChunkNum
);
99 inputTransport_
.seekToChunk(startChunkNum
);
100 processUntil(endChunkNum
);
104 * Process a single chunk
106 * @param chunkNum chunk to be processed
108 public void processChunk(int chunkNum
) throws TException
{
109 processChunk(chunkNum
, chunkNum
);
113 * Process a current chunk
115 public void processChunk() throws TException
{
116 processChunk(inputTransport_
.getCurChunk());