Search in sources :

Example 16 with EditableBinaryData

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);
}
Also used : CharsetCapable(org.exbin.deltahex.capability.CharsetCapable) EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) Charset(java.nio.charset.Charset)

Example 17 with EditableBinaryData

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;
    }
}
Also used : EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData)

Example 18 with EditableBinaryData

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;
}
Also used : EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData)

Example 19 with EditableBinaryData

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);
            }
        }
    }
}
Also used : JFileChooser(javax.swing.JFileChooser) EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 20 with EditableBinaryData

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);
        }
    }
}
Also used : EditableBinaryData(org.exbin.utils.binary_data.EditableBinaryData) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

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