Ejemplo n.º 1
0
  private synchronized BTreeChunk readChunkHeader(int chunkId) {
    FileStorage fileStorage = getFileStorage(chunkId);

    BTreeChunk chunk = null;
    ByteBuffer chunkHeaderBlocks = fileStorage.readFully(0, CHUNK_HEADER_SIZE);
    byte[] buff = new byte[BLOCK_SIZE];
    for (int i = 0; i <= BLOCK_SIZE; i += BLOCK_SIZE) {
      chunkHeaderBlocks.get(buff);
      try {
        String s = new String(buff, 0, BLOCK_SIZE, DataUtils.LATIN).trim();
        HashMap<String, String> m = DataUtils.parseMap(s);
        int blockSize = DataUtils.readHexInt(m, "blockSize", BLOCK_SIZE);
        if (blockSize != BLOCK_SIZE) {
          throw DataUtils.newIllegalStateException(
              DataUtils.ERROR_UNSUPPORTED_FORMAT,
              "Block size {0} is currently not supported",
              blockSize);
        }
        int check = DataUtils.readHexInt(m, "fletcher", 0);
        m.remove("fletcher");
        s = s.substring(0, s.lastIndexOf("fletcher") - 1);
        byte[] bytes = s.getBytes(DataUtils.LATIN);
        int checksum = DataUtils.getFletcher32(bytes, bytes.length);
        if (check != checksum) {
          continue;
        }
        chunk = BTreeChunk.fromString(s);
        break;
      } catch (Exception e) {
        continue;
      }
    }
    if (chunk == null) {
      throw DataUtils.newIllegalStateException(
          DataUtils.ERROR_FILE_CORRUPT, "Storage header is corrupt: {0}", fileStorage);
    }

    chunk.fileStorage = fileStorage;
    chunks.put(chunk.id, chunk);
    return chunk;
  }