@Override
protected void toStreamLockSafe(ObjectOutput objectOutput) throws CacheLoaderException {
try {
File[] files = root.listFiles();
if (files == null)
throw new CacheLoaderException("Root not directory or IO error occurred");
objectOutput.writeInt(files.length);
byte[] buffer = new byte[streamBufferSize];
for (File file : files) {
int bytesRead, totalBytesRead = 0;
BufferedInputStream bis = null;
FileInputStream fileInStream = null;
try {
if (trace) {
log.tracef("Opening file in %s", file);
}
fileInStream = new FileInputStream(file);
int sz = fileInStream.available();
bis = new BufferedInputStream(fileInStream);
objectOutput.writeObject(file.getName());
objectOutput.writeInt(sz);
while (sz > totalBytesRead) {
bytesRead = bis.read(buffer, 0, streamBufferSize);
if (bytesRead == -1) {
break;
}
totalBytesRead += bytesRead;
objectOutput.write(buffer, 0, bytesRead);
}
} finally {
Util.close(bis);
Util.close(fileInStream);
}
}
} catch (IOException e) {
throw new CacheLoaderException("I/O exception while generating stream", e);
}
}