use of org.exbin.utils.binary_data.EditableBinaryData in project deltahex-java by exbin.
the class InsertCharEditDataOperation 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);
data.insert(editedDataPosition, bytes);
length += bytes.length;
((CaretCapable) codeArea.getWorker()).getCaret().setCaretPosition(startPosition + length);
}
use of org.exbin.utils.binary_data.EditableBinaryData in project deltahex-java by exbin.
the class InsertCodeEditDataOperation method appendEdit.
@Override
public void appendEdit(byte value) {
EditableBinaryData data = (EditableBinaryData) codeArea.getData();
long editedDataPosition = startPosition + length;
byte byteValue = 0;
if (codeOffset > 0) {
byteValue = data.getByte(editedDataPosition - 1);
byte byteRest = 0;
switch(codeType) {
case BINARY:
{
byteRest = (byte) (byteValue & (0xff >> codeOffset));
break;
}
case DECIMAL:
{
byteRest = (byte) (byteValue % (codeOffset == 1 ? 100 : 10));
break;
}
case OCTAL:
{
byteRest = (byte) (byteValue % (codeOffset == 1 ? 64 : 8));
break;
}
case HEXADECIMAL:
{
byteRest = (byte) (byteValue & 0xf);
break;
}
default:
throw new IllegalStateException("Unexpected code type " + codeType.name());
}
if (byteRest > 0) {
if (trailing) {
throw new IllegalStateException("Unexpected trailing flag");
}
trailingValue = (EditableBinaryData) data.copy(editedDataPosition - 1, 1);
data.insert(editedDataPosition, 1);
data.setByte(editedDataPosition, byteRest);
byteValue -= byteRest;
trailing = true;
}
editedDataPosition--;
} else {
data.insert(editedDataPosition, 1);
length++;
}
switch(codeType) {
case BINARY:
{
int bitMask = 0x80 >> codeOffset;
byteValue = (byte) (byteValue & (0xff - bitMask) | (value << (7 - codeOffset)));
break;
}
case DECIMAL:
{
int newValue = byteValue & 0xff;
switch(codeOffset) {
case 0:
{
newValue = (newValue % 100) + value * 100;
if (newValue > 255) {
newValue = 200;
}
break;
}
case 1:
{
newValue = (newValue / 100) * 100 + value * 10 + (newValue % 10);
if (newValue > 255) {
newValue -= 200;
}
break;
}
case 2:
{
newValue = (newValue / 10) * 10 + value;
if (newValue > 255) {
newValue -= 200;
}
break;
}
}
byteValue = (byte) newValue;
break;
}
case OCTAL:
{
int newValue = byteValue & 0xff;
switch(codeOffset) {
case 0:
{
newValue = (newValue % 64) + value * 64;
break;
}
case 1:
{
newValue = (newValue / 64) * 64 + value * 8 + (newValue % 8);
break;
}
case 2:
{
newValue = (newValue / 8) * 8 + value;
break;
}
}
byteValue = (byte) newValue;
break;
}
case HEXADECIMAL:
{
if (codeOffset == 1) {
byteValue = (byte) ((byteValue & 0xf0) | value);
} else {
byteValue = (byte) ((byteValue & 0xf) | (value << 4));
}
break;
}
default:
throw new IllegalStateException("Unexpected code type " + codeType.name());
}
data.setByte(editedDataPosition, byteValue);
codeOffset++;
if (codeOffset == codeType.getMaxDigitsForByte()) {
codeOffset = 0;
}
}
use of org.exbin.utils.binary_data.EditableBinaryData in project deltahex-java by exbin.
the class RemoveDataOperation method execute.
private CodeAreaOperation execute(boolean withUndo) {
CodeAreaOperation undoOperation = null;
if (withUndo) {
EditableBinaryData undoData = (EditableBinaryData) codeArea.getData().copy(position, length);
undoOperation = new InsertDataOperation(codeArea, position, codeOffset, undoData);
}
((EditableBinaryData) codeArea.getData()).remove(position, length);
((CaretCapable) codeArea.getWorker()).getCaret().setCaretPosition(position, codeOffset);
return undoOperation;
}
use of org.exbin.utils.binary_data.EditableBinaryData in project deltahex-java by exbin.
the class DeltaHexEditorBasic method openFileActionPerformed.
private void openFileActionPerformed() {
if (releaseFile()) {
JFileChooser fileChooser = new JFileChooser();
int chooserResult = fileChooser.showOpenDialog(this);
if (chooserResult == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
try (FileInputStream stream = new FileInputStream(file)) {
((EditableBinaryData) codeArea.getData()).loadFromStream(stream);
codeArea.notifyDataChanged();
codeArea.repaint();
undoHandler.clear();
updateUndoState();
updateClipboardState();
} catch (FileNotFoundException ex) {
Logger.getLogger(DeltaHexEditorBasic.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(DeltaHexEditorBasic.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
use of org.exbin.utils.binary_data.EditableBinaryData in project deltahex-java by exbin.
the class DeltaHexEditorBasic method saveToFile.
public void saveToFile() {
if (file == null) {
saveAsFileActionPerformed();
} else {
try (FileOutputStream stream = new FileOutputStream(file)) {
((EditableBinaryData) codeArea.getData()).saveToStream(stream);
undoHandler.setSyncPoint();
} catch (FileNotFoundException ex) {
Logger.getLogger(DeltaHexEditorBasic.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(DeltaHexEditorBasic.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Aggregations