<commit_before>/**
 * Copyright (c) 2011, University of Konstanz, Distributed Systems Group
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the University of Konstanz nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.treetank.io.file;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.treetank.exception.TTIOException;
import org.treetank.io.IWriter;
import org.treetank.page.IPage;
import org.treetank.page.PagePersistenter;
import org.treetank.page.PageReference;

/**
 * File Writer for providing read/write access for file as a treetank backend.
 * 
 * @author <NAME>, Seabix
 * @author <NAME>, University of Konstanz
 * 
 */
public final class FileWriter implements IWriter {

    /** Random access mFile to work on. */
    private transient final RandomAccessFile mFile;

    /** Compressor to compress the page. */
    private transient final CryptoJavaImpl mCompressor;

    /** Reader instance for this writer. */
    private transient final FileReader reader;

    /**
     * Constructor.
     * 
     * 
     * @param paramStorage
     *            the Concrete Storage
     * @throws TTIOException
     *             if FileWriter IO error
     */
    public FileWriter(final File paramStorage) throws TTIOException {
        try {
            mFile = new RandomAccessFile(paramStorage, "rw");
        } catch (final FileNotFoundException fileExc) {
            throw new TTIOException(fileExc);
        }

        mCompressor = new CryptoJavaImpl();

        reader = new FileReader(paramStorage);

    }

    /**
     * Write page contained in page reference to storage.
     * 
     * @param pageReference
     *            Page reference to write.
     * @throws TTIOException
     *             due to errors during writing.
     */
    public long write(final PageReference pageReference) throws TTIOException {

        final ByteBufferSinkAndSource mBuffer = new ByteBufferSinkAndSource();
        mBuffer.position(FileReader.OTHER_BEACON);
        final IPage page = pageReference.getPage();
        final byte[] pagebytes = page.getByteRepresentation();
        for (byte val : pagebytes) {
            mBuffer.writeByte(val);
        }
//        PagePersistenter.serializePage(mBuffer, page);

        final int inputLength = mBuffer.position();

        // Perform crypto operations.
        final int outputLength = mCompressor.crypt(inputLength, mBuffer);
        if (outputLength == 0) {
            throw new TTIOException("Page crypt error.");
        }
        // normally, the first bytes until FileReader.OTHERBEACON are reserved
        // and cut of resulting in
        final byte[] tmp = new byte[outputLength];
        mBuffer.position(0);
        // Because of the missing offset, we can write the length directly at
        // the front of the buffer to see
        // it afterwards in the byte array as well. Do not forget to reset the
        // position before transition to
        // the array
        mBuffer.writeInt(outputLength);
        mBuffer.position(0);<commit_msg>[RM] PagePersistener from FileWriter<commit_after>/**
 * Copyright (c) 2011, University of Konstanz, Distributed Systems Group
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * * Neither the name of the University of Konstanz nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.treetank.io.file;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

import org.treetank.exception.TTIOException;
import org.treetank.io.IWriter;
import org.treetank.page.IPage;
import org.treetank.page.PageReference;

/**
 * File Writer for providing read/write access for file as a treetank backend.
 * 
 * @author <NAME>, Seabix
 * @author <NAME>, University of Konstanz
 * 
 */
public final class FileWriter implements IWriter {

    /** Random access mFile to work on. */
    private transient final RandomAccessFile mFile;

    /** Compressor to compress the page. */
    private transient final CryptoJavaImpl mCompressor;

    /** Reader instance for this writer. */
    private transient final FileReader reader;

    /**
     * Constructor.
     * 
     * 
     * @param paramStorage
     *            the Concrete Storage
     * @throws TTIOException
     *             if FileWriter IO error
     */
    public FileWriter(final File paramStorage) throws TTIOException {
        try {
            mFile = new RandomAccessFile(paramStorage, "rw");
        } catch (final FileNotFoundException fileExc) {
            throw new TTIOException(fileExc);
        }

        mCompressor = new CryptoJavaImpl();

        reader = new FileReader(paramStorage);

    }

    /**
     * Write page contained in page reference to storage.
     * 
     * @param pageReference
     *            Page reference to write.
     * @throws TTIOException
     *             due to errors during writing.
     */
    public long write(final PageReference pageReference) throws TTIOException {

        final ByteBufferSinkAndSource mBuffer = new ByteBufferSinkAndSource();
        mBuffer.position(FileReader.OTHER_BEACON);
        final IPage page = pageReference.getPage();
        final byte[] pagebytes = page.getByteRepresentation();
        for (byte val : pagebytes) {
            mBuffer.writeByte(val);
        }

        final int inputLength = mBuffer.position();

        // Perform crypto operations.
        final int outputLength = mCompressor.crypt(inputLength, mBuffer);
        if (outputLength == 0) {
            throw new TTIOException("Page crypt error.");
        }
        // normally, the first bytes until FileReader.OTHERBEACON are reserved
        // and cut of resulting in
        final byte[] tmp = new byte[outputLength];
        mBuffer.position(0);
        // Because of the missing offset, we can write the length directly at
        // the front of the buffer to see
        // it afterwards in the byte array as well. Do not forget to reset the
        // position before transition to
        // the array
        mBuffer.writeInt(outputLength);
        mBuffer.position(0);
{'fraction_non_alphanumeric': 0.06991358994501179, 'fraction_numerical': 0.0015710919088766694, 'mean_word_length': 2.9573712255772646, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3020515', 'n_tokens_mistral': 2349, 'n_tokens_neox': 2159, 'n_words': 930}