IntelHexParseRangeDetector added.

Minor changes.
This commit is contained in:
riilabs 2015-03-01 03:12:35 +02:00
parent 08c7e002b2
commit afe484a29e
4 changed files with 55 additions and 17 deletions

View File

@ -0,0 +1,31 @@
package cz.jaybee.intelhex;
public class IntelHexParseRangeDetector implements IntelHexDataListener {
private final MemoryRegions regions = new MemoryRegions();
@Override
public void data(long address, byte[] data) {
regions.add(address, data.length);
}
@Override
public void eof() {
regions.compact();
}
public void reset() {
regions.regions.clear();
}
public long getStart() {
if (regions.regions.size() == 0) return 0;
MemoryRegions.Region first = regions.regions.get(0);
return first.getStart();
}
public long getLength() {
if (regions.regions.size() == 0) return 0;
MemoryRegions.Region last = regions.regions.get(regions.regions.size() - 1);
return last.getStart() + last.getLength() - getStart();
}
}

View File

@ -36,7 +36,7 @@ import java.io.*;
*/
public class IntelHexParser {
private BufferedReader reader = null;
private final BufferedReader reader;
private IntelHexDataListener dataListener = null;
private static final int HEX = 16;
private boolean eof = false;
@ -70,11 +70,7 @@ public class IntelHexParser {
}
public IntelHexParser(Reader reader) {
if (reader instanceof BufferedReader) {
this.reader = (BufferedReader) reader;
} else {
this.reader = new BufferedReader(reader);
}
this.reader = (reader instanceof BufferedReader) ? (BufferedReader) reader : new BufferedReader(reader);
}
public IntelHexParser(InputStream stream) {
@ -197,9 +193,11 @@ public class IntelHexParser {
return startAddress;
}
public void parse() throws IOException, Exception {
public void parse() throws Exception {
eof = false;
recordIdx = 1;
upperAddress = 0;
startAddress = 0;
String recordStr;
while ((recordStr = reader.readLine()) != null) {

View File

@ -88,7 +88,7 @@ public class IntelHexParserDemo implements IntelHexDataListener {
System.out.println(ihpd.regions);
}
private IntelHexParserDemo(long addressStart, long addressStop, OutputStream destination) {
public IntelHexParserDemo(long addressStart, long addressStop, OutputStream destination) {
this.addressStart = addressStart;
this.addressStop = addressStop;
this.destination = destination;

View File

@ -37,13 +37,9 @@ import java.util.List;
*/
public class MemoryRegions {
List<Region> regions;
public final List<Region> regions = new ArrayList<>();
public MemoryRegions() {
regions = new ArrayList<Region>();
}
private class Region implements Comparable<Region>{
public static class Region implements Comparable<Region>{
private long start;
private long length;
@ -61,6 +57,10 @@ public class MemoryRegions {
return length;
}
void incLength(long value) {
length += value;
}
@Override
public String toString() {
return String.format("0x%08x:0x%08x (%dB 0x%08X)", start, start + length - 1, length, length);
@ -82,7 +82,7 @@ public class MemoryRegions {
prevRegion = regions.get(regions.size() - 1);
long nextAddress = prevRegion.start + prevRegion.length;
if (nextAddress == start) {
prevRegion.length += length;
prevRegion.incLength(length);
return;
}
}
@ -93,9 +93,18 @@ public class MemoryRegions {
Collections.sort(regions);
Iterator<Region> iter = regions.iterator();
Region prev = null;
while(iter.hasNext()) {
Region r = iter.next();
Region curr = iter.next();
if (prev == null) prev = curr;
else {
// check for chaining
if (curr.getStart() == (prev.getStart() + prev.getLength())) {
prev.incLength(curr.getLength());
iter.remove();
}
else prev = curr;
}
}
}