use of org.exbin.utils.binary_data.BinaryData in project deltahex-java by exbin.
the class DeltaDocumentSaveTest method testSwapReverseSaveDocument.
@Test
public void testSwapReverseSaveDocument() {
DeltaDocument document = openTempDeltaDocument();
assertEquals(SAMPLE_ALLBYTES_SIZE, document.getDataSize());
for (int i = 0; i < 128; i++) {
BinaryData copy1 = document.copy(i, 1);
BinaryData copy2 = document.copy(255 - i, 1);
document.replace(i, copy2);
document.validatePointerPosition();
document.replace(255 - i, copy1);
document.validatePointerPosition();
copy1.dispose();
copy2.dispose();
}
try {
document.save();
InputStream comparisionFile;
try (InputStream dataInputStream = document.getDataInputStream()) {
comparisionFile = new FileInputStream(DeltaDocumentSaveTest.class.getResource(SAMPLE_REVERSE).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.BinaryData in project deltahex-java by exbin.
the class DeltaDocumentSaveTest method testOverwriteCopyBeginSaveDocument.
@Test
public void testOverwriteCopyBeginSaveDocument() {
DeltaDocument document = openTempDeltaDocument();
assertEquals(SAMPLE_ALLBYTES_SIZE, document.getDataSize());
BinaryData copy = document.copy(0x40, 2);
document.replace(0, copy);
document.validatePointerPosition();
copy.dispose();
try {
document.save();
InputStream comparisionFile;
try (InputStream dataInputStream = document.getDataInputStream()) {
comparisionFile = new FileInputStream(DeltaDocumentSaveTest.class.getResource(SAMPLE_OVERWRITTEN_BEGIN).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.BinaryData 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.BinaryData in project deltahex-java by exbin.
the class DefaultCodeAreaPainter method paintCursor.
@Override
public void paintCursor(@Nonnull Graphics g) {
if (!worker.getCodeArea().hasFocus()) {
return;
}
DefaultCodeAreaCaret caret = (DefaultCodeAreaCaret) ((CaretCapable) worker).getCaret();
Rectangle cursorRect = getPositionRect(caret.getDataPosition(), caret.getCodeOffset(), caret.getSection());
if (cursorRect == null) {
return;
}
Rectangle clipBounds = g.getClipBounds();
Rectangle mainAreaRect = getMainAreaRect();
Rectangle intersection = mainAreaRect.intersection(cursorRect);
boolean cursorVisible = caret.isCursorVisible() && !intersection.isEmpty();
if (cursorVisible) {
g.setClip(intersection);
DefaultCodeAreaCaret.CursorRenderingMode renderingMode = caret.getRenderingMode();
g.setColor(colors.cursor);
switch(renderingMode) {
case PAINT:
{
g.fillRect(intersection.x, intersection.y, intersection.width, intersection.height);
break;
}
case XOR:
{
g.setXORMode(colors.background);
g.fillRect(intersection.x, intersection.y, intersection.width, intersection.height);
g.setPaintMode();
break;
}
case NEGATIVE:
{
g.fillRect(cursorRect.x, cursorRect.y, cursorRect.width, cursorRect.height);
g.setColor(colors.negativeCursor);
BinaryData codeAreaData = worker.getCodeArea().getData();
int row = (cursorRect.y + scrollPosition.getScrollRowOffset() - dataViewY) / rowHeight;
int scrolledX = cursorRect.x + scrollPosition.getScrollCharPosition() * characterWidth + scrollPosition.getScrollCharOffset();
int posY = dataViewY + (row + 1) * rowHeight - subFontSpace - scrollPosition.getScrollRowOffset();
long dataPosition = caret.getDataPosition();
if (viewMode != CodeAreaViewMode.CODE_MATRIX && caret.getSection() == BasicCodeAreaSection.TEXT_PREVIEW.getSection()) {
int charPos = (scrolledX - previewRelativeX) / characterWidth;
if (dataPosition >= dataSize) {
break;
}
char[] previewChars = new char[1];
byte[] data = new byte[maxCharLength];
if (maxCharLength > 1) {
int charDataLength = maxCharLength;
if (dataPosition + maxCharLength > dataSize) {
charDataLength = (int) (dataSize - dataPosition);
}
if (codeAreaData == null) {
previewChars[0] = ' ';
} else {
codeAreaData.copyToArray(dataPosition, data, 0, charDataLength);
String displayString = new String(data, 0, charDataLength, charset);
if (!displayString.isEmpty()) {
previewChars[0] = displayString.charAt(0);
}
}
} else {
if (charMappingCharset == null || charMappingCharset != charset) {
buildCharMapping(charset);
}
if (codeAreaData == null) {
previewChars[0] = ' ';
} else {
previewChars[0] = charMapping[codeAreaData.getByte(dataPosition) & 0xFF];
}
}
int posX = previewRelativeX + charPos * characterWidth - scrollPosition.getScrollCharPosition() * characterWidth - scrollPosition.getScrollCharOffset();
// if (characterRenderingMode == CharacterRenderingMode.LINE_AT_ONCE) {
// g.drawChars(previewChars, 0, 1, posX, posY);
// } else {
drawCenteredChar(g, previewChars, 0, characterWidth, posX, posY);
// }
} else {
int charPos = (scrolledX - dataViewX) / characterWidth;
int byteOffset = computePositionByte(charPos);
int codeCharPos = computeFirstCodeCharacterPos(byteOffset);
char[] rowChars = new char[codeType.getMaxDigitsForByte()];
if (codeAreaData != null && dataPosition < dataSize) {
byte dataByte = codeAreaData.getByte(dataPosition);
CodeAreaUtils.byteToCharsCode(dataByte, codeType, rowChars, 0, hexCharactersCase);
} else {
Arrays.fill(rowChars, ' ');
}
int posX = dataViewX + codeCharPos * characterWidth - scrollPosition.getScrollCharPosition() * characterWidth - scrollPosition.getScrollCharOffset();
int charsOffset = charPos - codeCharPos;
// if (characterRenderingMode == CharacterRenderingMode.LINE_AT_ONCE) {
// g.drawChars(lineChars, charsOffset, 1, posX + (charsOffset * characterWidth), posY);
// } else {
drawCenteredChar(g, rowChars, charsOffset, characterWidth, posX + (charsOffset * characterWidth), posY);
// }
}
break;
}
default:
throw new IllegalStateException("Unexpected rendering mode " + renderingMode.name());
}
}
// Paint mirror cursor
if (viewMode == CodeAreaViewMode.DUAL && showMirrorCursor) {
Rectangle mirrorCursorRect = getMirrorCursorRect(caret.getDataPosition(), caret.getSection());
if (mirrorCursorRect != null) {
intersection = mainAreaRect.intersection(mirrorCursorRect);
boolean mirrorCursorVisible = !intersection.isEmpty();
if (mirrorCursorVisible) {
g.setClip(intersection);
g.setColor(colors.cursor);
Graphics2D g2d = (Graphics2D) g.create();
Stroke dashed = new BasicStroke(1, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[] { 2 }, 0);
g2d.setStroke(dashed);
g2d.drawRect(mirrorCursorRect.x, mirrorCursorRect.y, mirrorCursorRect.width - 1, mirrorCursorRect.height - 1);
}
}
}
g.setClip(clipBounds);
}
use of org.exbin.utils.binary_data.BinaryData in project deltahex-java by exbin.
the class DefaultCodeAreaCommandHandler method setCodeValue.
private void setCodeValue(int value) {
CaretPosition caretPosition = ((CaretCapable) codeArea.getWorker()).getCaret().getCaretPosition();
long dataPosition = caretPosition.getDataPosition();
int codeOffset = caretPosition.getCodeOffset();
BinaryData data = codeArea.getData();
CodeType codeType = getCodeType();
byte byteValue = data.getByte(dataPosition);
byte outputValue = CodeAreaUtils.setCodeValue(byteValue, value, codeOffset, codeType);
((EditableBinaryData) data).setByte(dataPosition, outputValue);
}
Aggregations