/** * Seek to the given offset from the start of the file. The next read() will be from that * location. Can't seek past the end of the file. */ @Override public void seek(long pos) throws IOException { if (pos == mCurrentPosition) { return; } if (pos < mCurrentPosition) { throw new IOException( "Not supported to seek to " + pos + " . Current Position is " + mCurrentPosition); } if (mTachyonFileInputStream != null) { long needSkip = pos - mCurrentPosition; while (needSkip > 0) { needSkip -= mTachyonFileInputStream.skip(needSkip); } mCurrentPosition = pos; } else if (mHdfsInputStream != null) { mHdfsInputStream.seek(pos); mCurrentPosition = pos; } }
/** * This function displays 5KB of a file from a specific offset if it is in ASCII format. * * @param path The path of the file to display * @param request The HttpServletRequest object * @param offset Where the file starts to display. * @throws FileDoesNotExistException * @throws IOException * @throws InvalidPathException */ private void displayFile(String path, HttpServletRequest request, long offset) throws FileDoesNotExistException, InvalidPathException, IOException { String masterAddress = Constants.HEADER + mMasterInfo.getMasterAddress().getHostName() + ":" + mMasterInfo.getMasterAddress().getPort(); TachyonFS tachyonClient = TachyonFS.get(masterAddress); TachyonFile tFile = tachyonClient.getFile(path); String fileData = null; if (tFile == null) { throw new FileDoesNotExistException(path); } if (tFile.isComplete()) { InStream is = tFile.getInStream(ReadType.NO_CACHE); int len = (int) Math.min(5 * Constants.KB, tFile.length() - offset); byte[] data = new byte[len]; is.skip(offset); is.read(data, 0, len); fileData = CommonUtils.convertByteArrayToStringWithoutEscape(data); if (fileData == null) { fileData = "The requested file is not completely encoded in ascii"; } is.close(); } else { fileData = "The requested file is not complete yet."; } try { tachyonClient.close(); } catch (IOException e) { LOG.error(e.getMessage()); } List<BlockInfo> rawBlockList = mMasterInfo.getBlockList(path); List<UiBlockInfo> uiBlockInfo = new ArrayList<UiBlockInfo>(); for (BlockInfo blockInfo : rawBlockList) { uiBlockInfo.add(new UiBlockInfo(blockInfo)); } request.setAttribute("fileBlocks", uiBlockInfo); request.setAttribute("fileData", fileData); }