*/
public void writeHome(Home home, String name) throws RecorderException {
File homeFile = new File(name);
if (homeFile.exists()
&& !homeFile.canWrite()) {
throw new RecorderException("Can't write over file " + name);
}
DefaultHomeOutputStream homeOut = null;
File tempFile = null;
try {
// Open a stream on a temporary file
tempFile = OperatingSystem.createTemporaryFile("save", ".sweethome3d");
homeOut = new DefaultHomeOutputStream(new FileOutputStream(tempFile),
this.compressionLevel, this.includeOnlyTemporaryContent);
// Write home with HomeOuputStream
homeOut.writeHome(home);
} catch (InterruptedIOException ex) {
throw new InterruptedRecorderException("Save " + name + " interrupted");
} catch (IOException ex) {
throw new RecorderException("Can't save home " + name, ex);
} finally {
try {
if (homeOut != null) {
homeOut.close();
}
} catch (IOException ex) {
throw new RecorderException("Can't close temporary file " + name, ex);
}
}
// Open destination file
OutputStream out;
try {
out = new FileOutputStream(homeFile);
} catch (FileNotFoundException ex) {
if (tempFile != null) {
tempFile.delete();
}
throw new RecorderException("Can't save file " + name, ex);
}
// Copy temporary file to home file
// Overwriting home file will ensure that its rights are kept
byte [] buffer = new byte [8192];
InputStream in = null;
try {
in = new FileInputStream(tempFile);
int size;
while ((size = in.read(buffer)) != -1) {
out.write(buffer, 0, size);
}
} catch (IOException ex) {
throw new RecorderException("Can't copy file " + tempFile + " to " + name);
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException ex) {
throw new RecorderException("Can't close file " + name, ex);
}
try {
if (in != null) {
in.close();
tempFile.delete();