printUsage(); } catch (Exception e) { System.err.println("Cannot parse " + args[1]); printUsage(); TFileTransport t = new TFileTransport(args[0], true); t.open(); System.out.println("NumChunks="+t.getNumChunks()); int cnum = r.nextInt(t.getNumChunks()-1); System.out.println("Reading chunk "+cnum); t.seekToChunk(cnum); for(int i=0; i<4096; i++) { t.read(buf, 0, 4096);
/** * Process a current chunk */ public void processChunk() throws TException { processChunk(inputTransport_.getCurChunk()); } }
public void seekToEnd() throws TTransportException { if(!isOpen()) throw new TTransportException(TTransportException.NOT_OPEN, "Must open before seeking"); seekToChunk(getNumChunks()); }
/** * Event is corrupted. Do recovery * * @return true if recovery could be performed and we can read more data * false is returned only when nothing more can be read */ private boolean performRecovery() throws TTransportException { int numChunks = getNumChunks(); int curChunk = cs.getChunkNum(); if(curChunk >= (numChunks-1)) { return false; } seekToChunk(curChunk+1); return true; }
public void seekToChunk(int chunk) throws TTransportException { if(!isOpen()) throw new TTransportException(TTransportException.NOT_OPEN, "Must open before seeking"); int numChunks = getNumChunks(); inputStream_ = createInputStream(); tailPolicy old = setTailPolicy(tailPolicy.WAIT_FOREVER); while(cs.getOffset() < eofOffset) { readEvent(); } currentEvent_.setAvailable(0); setTailPolicy(old);
/** * Diverging from the cpp model and sticking to the TSocket model * Files are not opened in ctor - but in explicit open call */ public void open() throws TTransportException { if (isOpen()) throw new TTransportException(TTransportException.ALREADY_OPEN); try { inputStream_ = createInputStream(); cs = new chunkState(); currentEvent_ = new Event(new byte [256]); if(!readOnly_) outputStream_ = new BufferedOutputStream(inputFile_.getOutputStream(), 8192); } catch (IOException iox) { throw new TTransportException(TTransportException.NOT_OPEN, iox); } }
/** * Reads up to len bytes into buffer buf, starting at offset off. * * @param buf Array to read into * @param off Index to start reading at * @param len Maximum number of bytes to read * @return The number of bytes actually read * @throws TTransportException if there was an error reading data */ public int read(byte[] buf, int off, int len) throws TTransportException { if(!isOpen()) throw new TTransportException(TTransportException.NOT_OPEN, "Must open before reading"); if(currentEvent_.getRemaining() == 0) { if(!readEvent()) return(0); } int nread = currentEvent_.emit(buf, off, len); return nread; }
public int getCurChunk() throws TTransportException { if(!isOpen()) throw new TTransportException(TTransportException.NOT_OPEN, "Must open before getCurChunk"); return (cs.getChunkNum()); }
/** * Cloned from TTransport.java:readAll(). Only difference is throwing an EOF exception * where one is detected. */ public int readAll(byte[] buf, int off, int len) throws TTransportException { int got = 0; int ret = 0; while (got < len) { ret = read(buf, off+got, len-got); if (ret < 0) { throw new TTransportException("Error in reading from file"); } if(ret == 0) { throw new TTransportException(TTransportException.END_OF_FILE, "End of File reached"); } got += ret; } return got; }
public void seekToChunk(int chunk) throws TTransportException { if(!isOpen()) throw new TTransportException(TTransportException.NOT_OPEN, "Must open before seeking"); int numChunks = getNumChunks(); inputStream_ = createInputStream(); tailPolicy old = setTailPolicy(tailPolicy.WAIT_FOREVER); while(cs.getOffset() < eofOffset) { readEvent(); } currentEvent_.setAvailable(0); setTailPolicy(old);
/** * Event is corrupted. Do recovery * * @return true if recovery could be performed and we can read more data * false is returned only when nothing more can be read */ private boolean performRecovery() throws TTransportException { int numChunks = getNumChunks(); int curChunk = cs.getChunkNum(); if(curChunk >= (numChunks-1)) { return false; } seekToChunk(curChunk+1); return true; }
/** * Diverging from the cpp model and sticking to the TSocket model * Files are not opened in ctor - but in explicit open call */ public void open() throws TTransportException { if (isOpen()) throw new TTransportException(TTransportException.ALREADY_OPEN); try { inputStream_ = createInputStream(); cs = new chunkState(); currentEvent_ = new Event(new byte [256]); if(!readOnly_) outputStream_ = new BufferedOutputStream(inputFile_.getOutputStream(), 8192); } catch (IOException iox) { throw new TTransportException(TTransportException.NOT_OPEN, iox); } }
/** * Reads up to len bytes into buffer buf, starting at offset off. * * @param buf Array to read into * @param off Index to start reading at * @param len Maximum number of bytes to read * @return The number of bytes actually read * @throws TTransportException if there was an error reading data */ public int read(byte[] buf, int off, int len) throws TTransportException { if(!isOpen()) throw new TTransportException(TTransportException.NOT_OPEN, "Must open before reading"); if(currentEvent_.getRemaining() == 0) { if(!readEvent()) return(0); } int nread = currentEvent_.emit(buf, off, len); return nread; }
public int getCurChunk() throws TTransportException { if(!isOpen()) throw new TTransportException(TTransportException.NOT_OPEN, "Must open before getCurChunk"); return (cs.getChunkNum()); }
/** * Cloned from TTransport.java:readAll(). Only difference is throwing an EOF exception * where one is detected. */ public int readAll(byte[] buf, int off, int len) throws TTransportException { int got = 0; int ret = 0; while (got < len) { ret = read(buf, off+got, len-got); if (ret < 0) { throw new TTransportException("Error in reading from file"); } if(ret == 0) { throw new TTransportException(TTransportException.END_OF_FILE, "End of File reached"); } got += ret; } return got; }
printUsage(); } catch (Exception e) { System.err.println("Cannot parse " + args[1]); printUsage(); TFileTransport t = new TFileTransport(args[0], true); t.open(); System.out.println("NumChunks="+t.getNumChunks()); int cnum = r.nextInt(t.getNumChunks()-1); System.out.println("Reading chunk "+cnum); t.seekToChunk(cnum); for(int i=0; i<4096; i++) { t.read(buf, 0, 4096);
public void seekToEnd() throws TTransportException { if(!isOpen()) throw new TTransportException(TTransportException.NOT_OPEN, "Must open before seeking"); seekToChunk(getNumChunks()); }
/** * Process from start to last chunk both inclusive where chunks begin from 0 * @param startChunkNum first chunk to be processed * @param endChunkNum last chunk to be processed */ public void processChunk(int startChunkNum, int endChunkNum) throws TException { int numChunks = inputTransport_.getNumChunks(); if(endChunkNum < 0) endChunkNum += numChunks; if(startChunkNum < 0) startChunkNum += numChunks; if(endChunkNum < startChunkNum) throw new TException("endChunkNum " + endChunkNum + " is less than " + startChunkNum); inputTransport_.seekToChunk(startChunkNum); processUntil(endChunkNum); }
public int getNumChunks() throws TTransportException { if(!isOpen()) throw new TTransportException(TTransportException.NOT_OPEN, "Must open before getNumChunks"); try { long len = inputFile_.length(); if(len == 0) return 0; else return (((int)(len/cs.getChunkSize())) + 1); } catch (IOException iox) { throw new TTransportException(iox.getMessage(), iox); } }
/** * Process a current chunk */ public void processChunk() throws TException { processChunk(inputTransport_.getCurChunk()); } }