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"); } } }
public long writeTo(WritableByteChannel channel, long maxBps) throws IOException { // format: // <format_version> <sig_len> <sig_data> { <idx_file_name_len> <idx_file_name> <idx_file_len> // <idx_file_data> }... long amount = 0; // format version amount += ChannelUtil.writeInt(channel, 1); // index signature ByteArrayOutputStream baos = new ByteArrayOutputStream(); _sig.save(baos); byte[] sigBytes = baos.toByteArray(); amount += ChannelUtil.writeLong(channel, (long) sigBytes.length); // data length amount += channel.write(ByteBuffer.wrap(sigBytes)); // data // index files Collection<String> fileNames = _snapshot.getFileNames(); amount += ChannelUtil.writeInt(channel, fileNames.size()); // number of files for (String fileName : fileNames) { amount += ChannelUtil.writeString(channel, fileName); amount += _dirMgr.transferFromFileToChannel(fileName, channel, maxBps); } return amount; }