Search in sources :

Example 1 with BinaryStreamDriver

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");
}
Also used : ExchangeRate(jgnash.engine.ExchangeRate) Reminder(jgnash.engine.recurring.Reminder) Config(jgnash.engine.Config) XStream(com.thoughtworks.xstream.XStream) BufferedOutputStream(java.io.BufferedOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) CommodityNode(jgnash.engine.CommodityNode) IOException(java.io.IOException) Logger(java.util.logging.Logger) ObjectOutputStream(java.io.ObjectOutputStream) StoredObjectComparator(jgnash.engine.StoredObjectComparator) RootAccount(jgnash.engine.RootAccount) StoredObject(jgnash.engine.StoredObject) Budget(jgnash.engine.budget.Budget) BinaryStreamDriver(com.thoughtworks.xstream.io.binary.BinaryStreamDriver) BufferedOutputStream(java.io.BufferedOutputStream) PureJavaReflectionProvider(com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)

Example 2 with BinaryStreamDriver

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();
    }
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) XStream(com.thoughtworks.xstream.XStream) BinaryStreamDriver(com.thoughtworks.xstream.io.binary.BinaryStreamDriver) IOException(java.io.IOException) ObjectInputStream(java.io.ObjectInputStream)

Example 3 with BinaryStreamDriver

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());
    }
}
Also used : XStream(com.thoughtworks.xstream.XStream) ObjectInputStream(java.io.ObjectInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream) BinaryStreamDriver(com.thoughtworks.xstream.io.binary.BinaryStreamDriver) ArrayList(java.util.ArrayList) List(java.util.List) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Aggregations

XStream (com.thoughtworks.xstream.XStream)3 BinaryStreamDriver (com.thoughtworks.xstream.io.binary.BinaryStreamDriver)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)2 ObjectInputStream (java.io.ObjectInputStream)2 ObjectOutputStream (java.io.ObjectOutputStream)2 OutputStream (java.io.OutputStream)2 ArrayList (java.util.ArrayList)2 PureJavaReflectionProvider (com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 List (java.util.List)1 Logger (java.util.logging.Logger)1 CommodityNode (jgnash.engine.CommodityNode)1 Config (jgnash.engine.Config)1 ExchangeRate (jgnash.engine.ExchangeRate)1 RootAccount (jgnash.engine.RootAccount)1 StoredObject (jgnash.engine.StoredObject)1 StoredObjectComparator (jgnash.engine.StoredObjectComparator)1 Budget (jgnash.engine.budget.Budget)1