Search in sources :

Example 1 with EditableBinaryData

use of org.exbin.utils.binary_data.EditableBinaryData in project deltahex-java by exbin.

the class DeltaDocumentSaveTest method testSwapLastQuarterSaveDocument.

@Test
public void testSwapLastQuarterSaveDocument() {
    DeltaDocument document = openTempDeltaDocument();
    assertEquals(SAMPLE_ALLBYTES_SIZE, document.getDataSize());
    EditableBinaryData quarterCopy = (EditableBinaryData) document.copy(192, 64);
    document.remove(192, 64);
    document.validatePointerPosition();
    document.insert(0, quarterCopy);
    document.validatePointerPosition();
    quarterCopy.dispose();
    try {
        document.save();
        InputStream comparisionFile;
        try (InputStream dataInputStream = document.getDataInputStream()) {
            comparisionFile = new FileInputStream(DeltaDocumentSaveTest.class.getResource(SAMPLE_SWAP_LAST_QUARTER).getFile());
            TestUtils.assertEqualsInputStream(comparisionFile, dataInputStream);
        }
        comparisionFile.close();
    } catch (IOException ex) {
        Logger.getLogger(DeltaDocumentSaveTest.class.getName()).log(Level.SEVERE, null, ex);
        fail("Exception: " + ex.getMessage());
    }
    document.validatePointerPosition();
    document.clear();
    assertEquals(0, document.getSegments().size());
    closeTempDeltaDocument(document);
}
Also used : EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 2 with EditableBinaryData

use of org.exbin.utils.binary_data.EditableBinaryData in project deltahex-java by exbin.

the class DeltaDocumentSaveTest method testSwapDoubleSaveDocument.

@Test
public void testSwapDoubleSaveDocument() {
    DeltaDocument document = openTempDeltaDocument();
    assertEquals(SAMPLE_ALLBYTES_SIZE, document.getDataSize());
    EditableBinaryData quarterCopy = (EditableBinaryData) document.copy(0, 64);
    document.remove(0, 64);
    document.validatePointerPosition();
    document.insert(64, quarterCopy);
    document.validatePointerPosition();
    quarterCopy.dispose();
    EditableBinaryData quarterCopy2 = (EditableBinaryData) document.copy(128, 64);
    document.remove(128, 64);
    document.validatePointerPosition();
    document.insert(192, quarterCopy2);
    document.validatePointerPosition();
    quarterCopy2.dispose();
    try {
        document.save();
        InputStream comparisionFile;
        try (InputStream dataInputStream = document.getDataInputStream()) {
            comparisionFile = new FileInputStream(DeltaDocumentSaveTest.class.getResource(SAMPLE_SWAP_DOUBLE).getFile());
            TestUtils.assertEqualsInputStream(comparisionFile, dataInputStream);
        }
        comparisionFile.close();
    } catch (IOException ex) {
        Logger.getLogger(DeltaDocumentSaveTest.class.getName()).log(Level.SEVERE, null, ex);
        fail("Exception: " + ex.getMessage());
    }
    document.validatePointerPosition();
    document.clear();
    assertEquals(0, document.getSegments().size());
    closeTempDeltaDocument(document);
}
Also used : EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 3 with EditableBinaryData

use of org.exbin.utils.binary_data.EditableBinaryData in project deltahex-java by exbin.

the class DeleteCodeEditDataOperation method appendEdit.

@Override
public void appendEdit(byte value) {
    EditableBinaryData data = (EditableBinaryData) codeArea.getData();
    switch(value) {
        case BACKSPACE_CHAR:
            {
                if (position > 0) {
                    position--;
                    if (undoData == null) {
                        undoData = (EditableBinaryData) data.copy(position, 1);
                    } else {
                        EditableBinaryData dataCopy = (EditableBinaryData) data.copy(position, 1);
                        undoData.insert(0, dataCopy);
                        dataCopy.dispose();
                    }
                    data.remove(position, 1);
                }
                break;
            }
        case DELETE_CHAR:
            {
                if (position < data.getDataSize()) {
                    if (undoData == null) {
                        undoData = (EditableBinaryData) data.copy(position, 1);
                    } else {
                        EditableBinaryData dataCopy = (EditableBinaryData) data.copy(position, 1);
                        undoData.insert(0, dataCopy);
                        dataCopy.dispose();
                    }
                    data.remove(position, 1);
                }
                break;
            }
        default:
            {
                throw new IllegalStateException("Unexpected character " + value);
            }
    }
    ((CaretCapable) codeArea.getWorker()).getCaret().setCaretPosition(position);
    codeArea.repaint();
}
Also used : EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData)

Example 4 with EditableBinaryData

use of org.exbin.utils.binary_data.EditableBinaryData in project deltahex-java by exbin.

the class ModifyDataOperation method execute.

private CodeAreaOperation execute(boolean withUndo) {
    CodeAreaOperation undoOperation = null;
    if (withUndo) {
        BinaryData undoData = codeArea.getData().copy(position, data.getDataSize());
        undoOperation = new ModifyDataOperation(codeArea, position, undoData);
    }
    ((EditableBinaryData) codeArea.getData()).replace(position, data);
    return undoOperation;
}
Also used : EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) BinaryData(org.exbin.utils.binary_data.BinaryData)

Example 5 with EditableBinaryData

use of org.exbin.utils.binary_data.EditableBinaryData in project deltahex-java by exbin.

the class OverwriteCharEditDataOperation method appendEdit.

@Override
public void appendEdit(char value) {
    EditableBinaryData data = (EditableBinaryData) codeArea.getData();
    long editedDataPosition = startPosition + length;
    Charset charset = ((CharsetCapable) codeArea.getWorker()).getCharset();
    byte[] bytes = CodeAreaUtils.characterToBytes(value, charset);
    if (editedDataPosition < data.getDataSize()) {
        long overwritten = data.getDataSize() - editedDataPosition;
        if (overwritten > bytes.length) {
            overwritten = bytes.length;
        }
        EditableBinaryData overwrittenData = (EditableBinaryData) data.copy(editedDataPosition, overwritten);
        if (undoData == null) {
            undoData = overwrittenData;
        } else {
            undoData.insert(undoData.getDataSize(), overwrittenData);
        }
        for (int i = 0; i < overwritten; i++) {
            data.setByte(editedDataPosition + i, bytes[i]);
        }
    }
    if (editedDataPosition + bytes.length > data.getDataSize()) {
        if (editedDataPosition == data.getDataSize()) {
            data.insert(editedDataPosition, bytes);
        } else {
            int inserted = (int) (editedDataPosition + bytes.length - data.getDataSize());
            long insertPosition = editedDataPosition + bytes.length - inserted;
            data.insert(insertPosition, inserted);
            for (int i = 0; i < inserted; i++) {
                data.setByte(insertPosition + i, bytes[bytes.length - inserted + i]);
            }
        }
    }
    length += bytes.length;
    ((CaretCapable) codeArea.getWorker()).getCaret().setCaretPosition(startPosition + length);
}
Also used : CharsetCapable(org.exbin.deltahex.capability.CharsetCapable) EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) Charset(java.nio.charset.Charset)

Aggregations

EditableBinaryData (org.exbin.utils.binary_data.EditableBinaryData)27 Test (org.junit.Test)11 IOException (java.io.IOException)10 FileInputStream (java.io.FileInputStream)8 InputStream (java.io.InputStream)5 BinaryData (org.exbin.utils.binary_data.BinaryData)5 JFileChooser (javax.swing.JFileChooser)3 EditationModeCapable (org.exbin.deltahex.capability.EditationModeCapable)3 File (java.io.File)2 FileNotFoundException (java.io.FileNotFoundException)2 Charset (java.nio.charset.Charset)2 FileFilter (javax.swing.filechooser.FileFilter)2 CaretPosition (org.exbin.deltahex.CaretPosition)2 CodeType (org.exbin.deltahex.CodeType)2 CharsetCapable (org.exbin.deltahex.capability.CharsetCapable)2 DeltaHexExampleBasicPanel (org.exbin.deltahex.swing.example.DeltaHexExampleBasicPanel)2 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 FileOutputStream (java.io.FileOutputStream)1 SelectionRange (org.exbin.deltahex.SelectionRange)1 CaretCapable (org.exbin.deltahex.capability.CaretCapable)1