/** * This function displays the first 5KB of a file if it is in ASCII format. * * @param path The path of the file to display * @param request The HttpServletRequest object * @throws FileDoesNotExistException * @throws IOException * @throws InvalidPathException * @throws TException */ private void displayFile(String path, HttpServletRequest request) throws FileDoesNotExistException, InvalidPathException, IOException { TachyonClient tachyonClient = TachyonClient.getClient(mMasterInfo.getMasterAddress()); TachyonFile tFile = tachyonClient.getFile(path); if (tFile == null) { throw new FileDoesNotExistException(path); } InStream is = tFile.getInStream(OpType.READ_NO_CACHE); int len = Math.min(5 * Constants.KB, (int) tFile.getSize()); byte[] data = new byte[len]; is.read(data, 0, len); String fileData = CommonUtils.convertByteArrayToString(data); if (fileData == null) { fileData = "The requested file is not completely encoded in ascii"; } is.close(); try { tachyonClient.close(); } catch (TException e) { LOG.error(e.getMessage()); } request.setAttribute("fileData", fileData); return; }
@Override public void close() throws IOException { if (mTachyonFileInputStream != null) { mTachyonFileInputStream.close(); } if (mHdfsInputStream != null) { mHdfsInputStream.close(); } }
/** * 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); }