Example #1
0
  private File createTestFile(int length, int offset) throws IOException {
    File ret = createTestFile();

    byte[] data = new byte[length];
    rand.nextBytes(data);

    long crc32 = FileUtils.calcCRC(data);

    File baseDir = ret.getParentFile();
    String fileName = ret.getName();

    WriteRequest request = new WriteRequest();
    request.setBaseDir(baseDir);
    request.setFileName(fileName);
    request.setData(data);
    request.setOffset(offset);
    request.setLength(length);

    WriteResponse response = FileUtils.writeBlock(request);
    if (!response.isSuccess() || crc32 != response.getCrc32()) {
      throw new IOException(
          String.format("Failed creating temporary file: ", ret.getAbsolutePath()));
    }

    return ret;
  }
Example #2
0
  private void testCopyFile(
      BlockReader reader,
      String readFileName,
      BlockWriter writer,
      String writeFileName,
      File baseDir)
      throws IOException {
    ReadRequest readRequest = new ReadRequest();
    readRequest.setBaseDir(baseDir);
    readRequest.setFileName(readFileName);
    readRequest.setBlockSize(FileUtils.MIN_BLOCK_SIZE);

    WriteRequest writeRequest = new WriteRequest();
    writeRequest.setBaseDir(baseDir);
    writeRequest.setFileName(writeFileName);

    long offset = 0L;
    while (true) {
      readRequest.setOffset(offset);
      logger.info(String.format("Read Request Copy: %s", readRequest));
      ReadResponse readResponse = reader.readBlock(readRequest);
      logger.info(String.format("Read Response Copy: %s", readResponse));
      if (!readResponse.isSuccess()) {
        throw new ErrorReadingBlockException(
            String.format(
                "Error reading %d bytes '%s' in '%s' at offset of %d",
                readRequest.getBlockSize(),
                readRequest.getFileName(),
                readRequest.getBaseDir(),
                readRequest.getOffset()));
      }

      if (readResponse.getLength() > 0 || readResponse.isEof()) {
        writeRequest.setData(readResponse.getData());
        writeRequest.setOffset(offset);
        writeRequest.setLength(readResponse.getLength());
        writeRequest.setEof(readResponse.isEof());
        logger.info(String.format("Write Request Copy: %s", writeRequest));
        WriteResponse writeResponse = writer.writeBlock(writeRequest);
        logger.info(String.format("Write Response Copy: %s", writeResponse));
        if (!writeResponse.isSuccess()) {
          throw new ErrorWritingBlockException(
              String.format(
                  "Error writing %d bytes '%s' in '%s' at offset of %d",
                  writeRequest.getLength(),
                  writeRequest.getFileName(),
                  writeRequest.getBaseDir(),
                  writeRequest.getOffset()));
        }

        if (writeRequest.getLength() != writeResponse.getLength()) {
          throw new InvalidBlockHashException(
              String.format(
                  "Invalid length while writing what was expected. Exepecting %d but was %d",
                  writeRequest.getLength(), writeResponse.getLength()));
        } else if (readResponse.getLength() != writeResponse.getLength()) {
          throw new InvalidBlockHashException(
              String.format(
                  "Invalid length while writing what was read. Exepecting %d but was %d",
                  readResponse.getLength(), writeResponse.getLength()));
        }

        if (readResponse.getCrc32() != writeResponse.getCrc32()) {
          throw new InvalidBlockHashException(
              String.format(
                  "Invalid CRC while writing what was read. Exepecting %d but was %d",
                  readResponse.getCrc32(), writeResponse.getCrc32()));
        }

        offset += readResponse.getLength();
      }

      if (readResponse.isEof()) {
        break;
      }
    }
  }