don't copy but use the ByteArrayOutputStream's buffer for reading the SwissTiming archive contents in again

This commit is contained in:
Axel Uhl committed 2012-12-20 11:16:11 +01:00
1 parent ce6f9e277f
commit 7cb8fc33c8
3 files changed
+11 -9

No files matched your search

@@ -36,7 +36,9 @@ public interface SwissTimingReplayService {
* @param link
* the URL without the implicit "http://" prefix, as obtained, e.g., from
* {@link SwissTimingReplayRace#getLink()}.
* @param regatta TODO
* @param regatta
* the regatta to which the race shall be added; if <code>null</code>, a default regatta will be
* created/used
*/
void loadRaceData(String link, DomainFactory domainFactory, Regatta regatta, TrackedRegattaRegistry trackedRegattaRegistry);
@@ -71,23 +71,18 @@ public class SwissTimingReplayParserImpl implements SwissTimingReplayParser {
public void readData(InputStream urlInputStream, SwissTimingReplayListener replayListener) throws IOException,
UnknownMessageIdentificationCode, UnexpectedStartByte, PayloadMismatch, PrematureEndOfData {
DataInputStream data = new DataInputStream(urlInputStream);
int positionInStream = 0;
byte[] startByteBuffer = new byte[1];
int readSuccess = data.read(startByteBuffer);
while (readSuccess != -1) {
positionInStream += readSuccess;
byte startByte = startByteBuffer[0];
if (startByte != STX) {
throw new UnexpectedStartByte(startByte);
}
byte messageIdentificationCode = data.readByte();
positionInStream++;
short messageLength = data.readShort();
positionInStream += 2;
if (messageLength > 0) {
short payloadSize = (short) (messageLength - 5); // payload is message minus STX, MIC, message length
// and ETX/EOT --> = 5 byte
positionInStream += payloadSize;
switch (messageIdentificationCode) {
case MessageIdentificationCodes.MIC_00_Reference_Timestamp:
while (payloadSize > 0) {
@@ -242,7 +237,6 @@ public class SwissTimingReplayParserImpl implements SwissTimingReplayParser {
}
}
byte endByte = data.readByte();
positionInStream++;
if (endByte == EOT) {
replayListener.eot();
} else if (endByte == ETX) {
@@ -113,19 +113,25 @@ public class SwissTimingReplayServiceImpl implements SwissTimingReplayService {
return startTimeFormat;
}
private static class ByteArrayOutputStreamWithVisibleBuffer extends ByteArrayOutputStream {
public byte[] getBuffer() {
return buf;
}
}
@Override
public void loadRaceData(String link, SwissTimingReplayListener replayListener) {
URL raceDataUrl;
try {
raceDataUrl = new URL("http://" + link);
InputStream urlInputStream = (InputStream) raceDataUrl.getContent();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ByteArrayOutputStreamWithVisibleBuffer bos = new ByteArrayOutputStreamWithVisibleBuffer();
byte[] buf = new byte[8192];
int read;
while ((read = urlInputStream.read(buf)) != -1) {
bos.write(buf, 0, read);
}
new SwissTimingReplayParserImpl().readData(new ByteArrayInputStream(bos.toByteArray()), replayListener);
new SwissTimingReplayParserImpl().readData(new ByteArrayInputStream(bos.getBuffer(), 0, bos.size()), replayListener);
} catch (Exception e) {
throw new RuntimeException(e);
}