Example #1
0
  public void streamBlockInAscii(
      InetSocketAddress addr,
      long blockId,
      Token<BlockTokenIdentifier> accessToken,
      long genStamp,
      long blockSize,
      long offsetIntoBlock,
      long chunkSizeToView,
      JspWriter out,
      Configuration conf)
      throws IOException {
    if (chunkSizeToView == 0) return;
    Socket s = new Socket();
    s.connect(addr, HdfsConstants.READ_TIMEOUT);
    s.setSoTimeout(HdfsConstants.READ_TIMEOUT);

    long amtToRead = Math.min(chunkSizeToView, blockSize - offsetIntoBlock);

    // Use the block name for file name.
    BlockReader blockReader =
        RemoteBlockReader.newBlockReader(
            s,
            addr.toString() + ":" + blockId,
            blockId,
            accessToken,
            genStamp,
            offsetIntoBlock,
            amtToRead,
            conf.getInt("io.file.buffer.size", 4096));

    byte[] buf = new byte[(int) amtToRead];
    int readOffset = 0;
    int retries = 2;
    while (amtToRead > 0) {
      int numRead;
      try {
        numRead = blockReader.readAll(buf, readOffset, (int) amtToRead);
      } catch (IOException e) {
        retries--;
        if (retries == 0) throw new IOException("Could not read data from datanode");
        continue;
      }
      amtToRead -= numRead;
      readOffset += numRead;
    }
    blockReader = null;
    s.close();
    out.print(HtmlQuoting.quoteHtmlChars(new String(buf)));
  }