ValidationUtils.assertNotNull(reader, "reader cannot be null");
File directory = new File(getBaseDirectory(), subdirectory);
if (!directory.exists()) {
if (!directory.mkdirs())
throw new ContentServiceException("error creating subdirectory: " + directory.getAbsolutePath());
log.info("created directory " + directory.getAbsolutePath());
}
if (!directory.canWrite())
throw new ContentServiceException("subdirectory not writable: " + directory.getAbsolutePath());
UUID uuid = UUID.randomUUID();
String filename = uuid.toString();
if (extension != null)
filename = filename + "." + extension;
File outfile = new File(directory, filename);
if (outfile.exists())
throw new ContentServiceException("unique file already exists: " + outfile.getAbsolutePath());
FileWriter outWriter;
try {
outWriter = new FileWriter(outfile);
}
catch (IOException e) {
throw new ContentServiceException("error creating outfile: " + outfile.getAbsolutePath(), e);
}
int nread;
char buf[] = new char[bufferSize.get()];
try {
while ((nread = reader.read(buf)) != -1)
{
outWriter.write(buf, 0, nread);
}
outWriter.close();
log.info("saved content to " + outfile.getAbsolutePath());
return new ContentId(subdirectory, filename);
}
catch (IOException e) {
tryToClean(outfile, outWriter);
throw new ContentServiceException("error writing file: " + outfile.getAbsolutePath(), e);
}
}