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);
}
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);
}
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();
}
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;
}
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);
}
Aggregations