Example #1
0
  public static void readSnapshot(ReadableByteChannel channel, DirectoryManager dirMgr, long maxBps)
      throws IOException {
    // format version
    int formatVersion = ChannelUtil.readInt(channel);
    if (formatVersion != 1) {
      throw new IOException("snapshot format version mismatch [" + formatVersion + "]");
    }

    // index signature
    if (!dirMgr.transferFromChannelToFile(channel, DirectoryManager.INDEX_DIRECTORY)) {
      throw new IOException("bad snapshot file");
    }

    // index files
    int numFiles = ChannelUtil.readInt(channel); // number of files
    if (numFiles < 0) {
      throw new IOException("bad snapshot file");
    }
    while (numFiles-- > 0) {
      String fileName = ChannelUtil.readString(channel);
      if (fileName == null) {
        throw new IOException("bad snapshot file");
      }
      if (!dirMgr.transferFromChannelToFile(channel, fileName, maxBps)) {
        throw new IOException("bad snapshot file");
      }
    }
  }