use of com.thoughtworks.xstream.io.binary.BinaryStreamDriver in project jgnash by ccavanaugh.
the class BinaryContainer method writeBinary.
/**
* Writes an XML file given a collection of StoredObjects. TrashObjects and
* objects marked for removal are not written. If the file already exists,
* it will be overwritten.
*
* @param objects Collection of StoredObjects to write
* @param path file to write
*/
static synchronized void writeBinary(@NotNull final Collection<StoredObject> objects, @NotNull final Path path) {
final Logger logger = Logger.getLogger(BinaryContainer.class.getName());
if (!Files.exists(path.getParent())) {
try {
Files.createDirectories(path.getParent());
logger.info("Created missing directories");
} catch (final IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
}
createBackup(path);
List<StoredObject> list = new ArrayList<>();
list.addAll(query(objects, Budget.class));
list.addAll(query(objects, Config.class));
list.addAll(query(objects, CommodityNode.class));
list.addAll(query(objects, ExchangeRate.class));
list.addAll(query(objects, RootAccount.class));
list.addAll(query(objects, Reminder.class));
// remove any objects marked for removal
list.removeIf(StoredObject::isMarkedForRemoval);
// sort the list
list.sort(new StoredObjectComparator());
logger.info("Writing Binary file");
try (final OutputStream os = new BufferedOutputStream(Files.newOutputStream(path))) {
final XStream xstream = configureXStream(new XStreamOut(new PureJavaReflectionProvider(), new BinaryStreamDriver()));
try (final ObjectOutputStream out = xstream.createObjectOutputStream(os)) {
out.writeObject(list);
out.flush();
}
// forcibly flush before letting go of the resources to help older windows systems write correctly
os.flush();
} catch (IOException e) {
logger.log(Level.SEVERE, e.getLocalizedMessage(), e);
}
logger.info("Writing Binary file complete");
}
use of com.thoughtworks.xstream.io.binary.BinaryStreamDriver in project jgnash by ccavanaugh.
the class BinaryContainer method readBinary.
void readBinary() {
// A file lock will be held on Windows OS when reading
try (final InputStream fis = new BufferedInputStream(Files.newInputStream(path, StandardOpenOption.READ))) {
readWriteLock.writeLock().lock();
final XStream xstream = configureXStream(new XStream(new StoredObjectReflectionProvider(objects), new BinaryStreamDriver()));
// Filters out any java.sql.Dates that sneaked in when saving from a relational database
// and forces to a LocalDate
// TODO: Remove at a later date
xstream.alias("sql-date", LocalDate.class);
try (final ObjectInputStream in = xstream.createObjectInputStream(fis)) {
in.readObject();
}
} catch (final IOException | ClassNotFoundException e) {
Logger.getLogger(BinaryContainer.class.getName()).log(Level.SEVERE, null, e);
} finally {
if (!acquireFileLock()) {
// lock the file on open
Logger.getLogger(BinaryContainer.class.getName()).severe("Could not acquire the file lock");
}
readWriteLock.writeLock().unlock();
}
}
use of com.thoughtworks.xstream.io.binary.BinaryStreamDriver in project jgnash by ccavanaugh.
the class BinaryXStreamTest method testFile.
@Test
public void testFile() {
final List<String> stringData = new ArrayList<>();
for (int i = 0; i < 100; i++) {
stringData.add(UUID.randomUUID().toString());
}
final List<Integer> integerData = stringData.stream().map(String::hashCode).collect(Collectors.toList());
try {
tempFile = Files.createTempFile("test", "");
} catch (IOException e) {
fail(e.toString());
}
try (final OutputStream fos = Files.newOutputStream(tempFile)) {
BinaryStreamDriver bsd = new BinaryStreamDriver();
XStream xstream = new XStream(bsd);
try (ObjectOutputStream out = xstream.createObjectOutputStream(fos)) {
out.writeObject(stringData);
out.writeObject(integerData);
}
} catch (IOException e) {
fail(e.toString());
}
assertTrue(FileMagic.isBinaryXStreamFile(tempFile));
assertFalse(FileMagic.isOfxV2(tempFile));
try (InputStream fis = Files.newInputStream(tempFile)) {
BinaryStreamDriver bsd = new BinaryStreamDriver();
XStream xstream = new XStream(bsd);
try (ObjectInputStream in = xstream.createObjectInputStream(fis)) {
List<?> strings = (List<?>) in.readObject();
List<?> integers = (List<?>) in.readObject();
assertArrayEquals(strings.toArray(), stringData.toArray());
assertArrayEquals(integers.toArray(), integerData.toArray());
} catch (ClassNotFoundException e) {
fail(e.toString());
}
} catch (IOException e) {
fail(e.toString());
}
}
Aggregations