📄 sectionmotememory.java
字号:
setMemorySegment(varAddr, varData); } public byte getByteValueOf(String varName) throws UnknownVariableException { // Get start address of variable if (!variableAddresses.containsKey(varName)) { throw new UnknownVariableException(varName); } int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); byte[] varData = getMemorySegment(varAddr, 1); if (varData == null) { throw new UnknownVariableException(varName); } return varData[0]; } public void setByteValueOf(String varName, byte newVal) throws UnknownVariableException { // Get start address of variable if (!variableAddresses.containsKey(varName)) { throw new UnknownVariableException(varName); } int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); byte[] varData = new byte[1]; varData[0] = newVal; setMemorySegment(varAddr, varData); } public byte[] getByteArray(String varName, int length) throws UnknownVariableException { // Get start address of variable if (!variableAddresses.containsKey(varName)) { throw new UnknownVariableException(varName); } int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); // TODO Check if small/big-endian when coming from JNI? return getMemorySegment(varAddr, length); } public void setByteArray(String varName, byte[] data) throws UnknownVariableException { // Get start address of variable if (!variableAddresses.containsKey(varName)) { throw new UnknownVariableException(varName); } int varAddr = ((Integer) variableAddresses.get(varName)).intValue(); // TODO Check if small/big-endian when coming from JNI? setMemorySegment(varAddr, data); } /** * A memory section contains a byte array and a start address. * * @author Fredrik Osterlind */ private class MoteMemorySection { private byte[] data = null; private int startAddr; /** * Create a new memory section. * * @param startAddr * Start address of section * @param data * Data of section */ public MoteMemorySection(int startAddr, byte[] data) { this.startAddr = startAddr; this.data = data; } /** * Returns start address of this memory section. * * @return Start address */ public int getStartAddr() { return startAddr; } /** * Returns size of this memory section. * * @return Size */ public int getSize() { return data.length; } /** * Returns the entire byte array which defines this section. * * @return Byte array */ public byte[] getData() { return data; } /** * True if given address is part of this memory section. * * @param addr * Address * @return True if given address is part of this memory section, false * otherwise */ public boolean includesAddr(int addr) { if (data == null) { return false; } return (addr >= startAddr && addr < (startAddr + data.length)); } /** * Returns memory segment. * * @param addr * Start address of memory segment * @param size * Size of memory segment * @return Memory segment */ public byte[] getMemorySegment(int addr, int size) { byte[] ret = new byte[size]; System.arraycopy(data, addr - startAddr, ret, 0, size); return ret; } /** * Sets a memory segment. * * @param addr * Start of memory segment * @param data * Data of memory segment */ public void setMemorySegment(int addr, byte[] data) { System.arraycopy(data, 0, this.data, addr - startAddr, data.length); } public MoteMemorySection clone() { byte[] dataClone = new byte[data.length]; System.arraycopy(data, 0, dataClone, 0, data.length); MoteMemorySection clone = new MoteMemorySection(startAddr, dataClone); return clone; } } // EXPERIMENTAL AND DEBUG METHODS public SectionMoteMemory clone() { Vector<MoteMemorySection> clonedSections = new Vector<MoteMemorySection>(); for (MoteMemorySection section : sections) { clonedSections.add(section.clone()); } SectionMoteMemory clone = new SectionMoteMemory(variableAddresses); clone.sections = clonedSections; return clone; } protected byte[] getChecksum() { MessageDigest messageDigest; try { messageDigest = MessageDigest.getInstance("MD5"); for (MoteMemorySection section : sections) { messageDigest.update(section.data, 0, section.getSize()); } } catch (NoSuchAlgorithmException e) { return null; } return messageDigest.digest(); } protected Vector<Integer> getDifferenceAddressesOf( SectionMoteMemory anotherMem) { Vector<Integer> differences = new Vector<Integer>(); if (this.getNumberOfSections() != anotherMem.getNumberOfSections()) { logger.fatal("Number of section do not match!"); return null; } for (int i = 0; i < sections.size(); i++) { if (this.getSizeOfSection(i) != anotherMem.getSizeOfSection(i)) { logger.fatal("Section " + i + " sizes do not match!"); return null; } if (this.getStartAddrOfSection(i) != anotherMem.getStartAddrOfSection(i)) { logger.fatal("Section " + i + " start addresses do not match!"); return null; } for (int j = 0; j < sections.get(i).getSize(); j++) { if (this.sections.get(i).data[j] != anotherMem.getDataOfSection(i)[j]) { differences.add(new Integer(sections.get(i).startAddr + j)); } } System.err.println(); } return differences; } protected void printMemory() { for (int i = 0; i < sections.size(); i++) { System.err.print("Section[" + i + "]: "); for (int j = 0; j < sections.get(i).getSize(); j++) { System.err.print(sections.get(i).getData()[j] + ","); } System.err.println(); } } protected void printDifferences(SectionMoteMemory anotherMem) { if (this.getNumberOfSections() != anotherMem.getNumberOfSections()) { logger.fatal("Number of section do not match!"); return; } for (int i = 0; i < sections.size(); i++) { if (this.getSizeOfSection(i) != anotherMem.getSizeOfSection(i)) { logger.fatal("Section " + i + " sizes do not match!"); return; } if (this.getStartAddrOfSection(i) != anotherMem.getStartAddrOfSection(i)) { logger.fatal("Section " + i + " start addresses do not match!"); return; } System.err.print("Section[" + i + "]: "); for (int j = 0; j < sections.get(i).getSize(); j++) { if (this.sections.get(i).data[j] != anotherMem.getDataOfSection(i)[j]) { System.err.print(j + ","); } } System.err.println(); } } protected void printChecksum() { byte[] checksum = this.getChecksum(); System.err.print("Checksum: "); for (byte element : checksum) { System.err.print(element + ","); } System.err.println(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -