Skip to content

Commit 2741dc2

Browse files
authored
Merge pull request #197 from MaxKellermann/flush_each_line
aat-lib/OnState: flush the GPX file after every log() call
2 parents 65812fc + 75685c7 commit 2741dc2

File tree

9 files changed

+63
-24
lines changed

9 files changed

+63
-24
lines changed

aat-lib/src/main/java/ch/bailu/aat_lib/file/xml/writer/GpxListWriter.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,27 @@ class GpxListWriter(track: GpxList, file: Foc) : Closeable {
1616
writer.writeHeader(System.currentTimeMillis())
1717
}
1818

19+
/**
20+
* Flush internal buffers to the output file.
21+
*/
22+
@Throws(IOException::class)
23+
fun flush() {
24+
writer.flush()
25+
}
26+
1927
@Throws(IOException::class)
2028
override fun close() {
21-
flushOutput()
29+
writeNewPoints()
2230
writer.writeFooter()
2331
writer.close()
2432
}
2533

34+
/**
35+
* Write points that have been added to the #GpxList since the
36+
* last call to the #GpxWriter.
37+
*/
2638
@Throws(IOException::class)
27-
fun flushOutput() {
39+
fun writeNewPoints() {
2840
while (iterator.nextPoint()) {
2941
if (iterator.isFirstInSegment) {
3042
if (iterator.isFirstInTrack) {

aat-lib/src/main/java/ch/bailu/aat_lib/file/xml/writer/GpxWriter.kt

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ abstract class GpxWriter(file: Foc) {
3333
@Throws(IOException::class)
3434
abstract fun writeTrackPoint(tp: GpxPointInterface)
3535

36+
/**
37+
* Flush internal buffers to the output file.
38+
*/
39+
@Throws(IOException::class)
40+
fun flush() {
41+
output.flush()
42+
}
43+
3644
@Throws(IOException::class)
3745
fun close() {
3846
output.close()
@@ -50,25 +58,28 @@ abstract class GpxWriter(file: Foc) {
5058
writeString("</metadata>\n")
5159
}
5260

61+
@Throws(IOException::class)
62+
protected fun writeRawChar(ch: Char) {
63+
output.append(ch)
64+
}
65+
5366
@Throws(IOException::class)
5467
protected fun writeString(string: String) {
55-
output.write(string, 0, string.length)
68+
output.write(string)
5669
}
5770

5871
@Throws(IOException::class)
5972
protected fun writeTimeStamp(time: Long) {
60-
writeString(
61-
("<" + GpxConstants.QNAME_TIME + ">"
62-
+ f.dateFormat.format(time) +
63-
"</" + GpxConstants.QNAME_TIME + ">")
64-
)
73+
writeBeginElement(GpxConstants.QNAME_TIME);
74+
writeString(f.dateFormat.format(time));
75+
writeEndElement(GpxConstants.QNAME_TIME);
6576
}
6677

6778
@Throws(IOException::class)
6879
protected fun writeEndElement(e: String) {
6980
writeString("</")
7081
writeString(e)
71-
writeString(">")
82+
writeRawChar('>')
7283
}
7384

7485
@Throws(IOException::class)
@@ -78,13 +89,13 @@ abstract class GpxWriter(file: Foc) {
7889

7990
@Throws(IOException::class)
8091
protected fun writeBeginElementStart(e: String) {
81-
writeString("<")
92+
writeRawChar('<')
8293
writeString(e)
8394
}
8495

8596
@Throws(IOException::class)
8697
protected fun writeBeginElementEnd() {
87-
writeString(">")
98+
writeRawChar('>')
8899
}
89100

90101
@Throws(IOException::class)
@@ -95,11 +106,11 @@ abstract class GpxWriter(file: Foc) {
95106

96107
@Throws(IOException::class)
97108
protected fun writeParameter(parameterName: String, parameterValue: String) {
98-
writeString(" ")
109+
writeRawChar(' ')
99110
writeString(parameterName)
100111
writeString("=\"")
101112
writeString(xmlEscaper.escape(parameterValue))
102-
writeString("\"")
113+
writeRawChar('"')
103114
}
104115

105116
@Throws(IOException::class)

aat-lib/src/main/java/ch/bailu/aat_lib/file/xml/writer/RouteWriter.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class RouteWriter(file: Foc) : GpxWriter(file) {
1717
override fun writeFooter() {
1818
writeEndElement(GpxConstants.QNAME_ROUTE)
1919
writeEndElement(GpxConstants.QNAME_GPX)
20+
writeRawChar('\n')
2021
}
2122

2223
override fun writeSegment() {}
@@ -25,7 +26,7 @@ class RouteWriter(file: Foc) : GpxWriter(file) {
2526

2627
@Throws(IOException::class)
2728
override fun writeTrackPoint(tp: GpxPointInterface) {
28-
writeString("\t")
29+
writeRawChar('\t')
2930
writeBeginElementStart(GpxConstants.QNAME_ROUTE_POINT)
3031
writeParameter(GpxConstants.QNAME_LATITUDE, f.decimal6.format(tp.getLatitude()))
3132
writeParameter(GpxConstants.QNAME_LONGITUDE, f.decimal6.format(tp.getLongitude()))
@@ -38,6 +39,6 @@ class RouteWriter(file: Foc) : GpxWriter(file) {
3839
}
3940

4041
writeEndElement(GpxConstants.QNAME_ROUTE_POINT)
41-
writeString("\n")
42+
writeRawChar('\n')
4243
}
4344
}

aat-lib/src/main/java/ch/bailu/aat_lib/file/xml/writer/TrackWriter.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ class TrackWriter(file: Foc) : GpxWriter(file) {
1717
writeEndElement(GpxConstants.QNAME_TRACK_SEGMENT)
1818
writeEndElement(GpxConstants.QNAME_TRACK)
1919
writeEndElement(GpxConstants.QNAME_GPX)
20+
writeRawChar('\n')
2021
}
2122

2223
@Throws(IOException::class)
2324
override fun writeTrackPoint(tp: GpxPointInterface) {
24-
writeString("\t")
25+
writeRawChar('\t')
2526
writeBeginElementStart(GpxConstants.QNAME_TRACK_POINT)
2627
writeParameter(GpxConstants.QNAME_LATITUDE, f.decimal6.format(tp.getLatitude()))
2728
writeParameter(GpxConstants.QNAME_LONGITUDE, f.decimal6.format(tp.getLongitude()))
@@ -36,20 +37,20 @@ class TrackWriter(file: Foc) : GpxWriter(file) {
3637
writeAttributesGpxStyle(tp)
3738

3839
writeEndElement(GpxConstants.QNAME_TRACK_POINT)
39-
writeString("\n")
40+
writeRawChar('\n')
4041
}
4142

4243
@Throws(IOException::class)
4344
override fun writeFirstSegment() {
4445
writeBeginElement(GpxConstants.QNAME_TRACK_SEGMENT)
45-
writeString("\n")
46+
writeRawChar('\n')
4647
}
4748

4849

4950
@Throws(IOException::class)
5051
override fun writeSegment() {
5152
writeEndElement(GpxConstants.QNAME_TRACK_SEGMENT)
5253
writeBeginElement(GpxConstants.QNAME_TRACK_SEGMENT)
53-
writeString("\n")
54+
writeRawChar('\n')
5455
}
5556
}

aat-lib/src/main/java/ch/bailu/aat_lib/file/xml/writer/WayWriter.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ open class WayWriter(file: Foc) : GpxWriter(file) {
1010
@Throws(IOException::class)
1111
override fun writeFooter() {
1212
writeEndElement(GpxConstants.QNAME_GPX)
13+
writeRawChar('\n')
1314
}
1415

1516
override fun writeSegment() {
@@ -20,7 +21,7 @@ open class WayWriter(file: Foc) : GpxWriter(file) {
2021

2122
@Throws(IOException::class)
2223
override fun writeTrackPoint(tp: GpxPointInterface) {
23-
writeString("\t")
24+
writeRawChar('\t')
2425
writeBeginElementStart(GpxConstants.QNAME_WAY_POINT)
2526
writeParameter(GpxConstants.QNAME_LATITUDE, f.decimal6.format(tp.getLatitude()))
2627
writeParameter(GpxConstants.QNAME_LONGITUDE, f.decimal6.format(tp.getLongitude()))
@@ -31,7 +32,7 @@ open class WayWriter(file: Foc) : GpxWriter(file) {
3132
writeAttributes(tp)
3233

3334
writeEndElement(GpxConstants.QNAME_WAY_POINT)
34-
writeString("\n")
35+
writeRawChar('\n')
3536
}
3637

3738
@Throws(IOException::class)

aat-lib/src/main/java/ch/bailu/aat_lib/service/tracker/Logger.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ abstract class Logger : GpxInformation(), Closeable {
1515
@Throws(IOException::class)
1616
abstract fun log(tp: GpxPointInterface, attr: GpxAttributes)
1717

18+
@Throws(IOException::class)
19+
abstract fun flush()
20+
1821
override fun close() {}
1922

2023
fun setState(s: Int) {
@@ -28,6 +31,7 @@ abstract class Logger : GpxInformation(), Closeable {
2831
companion object {
2932
val NULL_LOGGER: Logger = object : Logger() {
3033
override fun log(tp: GpxPointInterface, attr: GpxAttributes) {}
34+
override fun flush() {}
3135
}
3236
}
3337
}

aat-lib/src/main/java/ch/bailu/aat_lib/service/tracker/OnState.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ class OnState(tracker: TrackerInternals) : State(tracker) {
3333
if (newLocation != null) {
3434
location = newLocation
3535
val attr = attributes.collect(internal.services.getSensorService())
36-
internal.logger.log(location, attr)
36+
internal.logger.log(newLocation, attr)
37+
internal.logger.flush()
3738
}
3839
} catch (e: IOException) {
3940
internal.emergencyOff(e)

aat-lib/src/main/java/ch/bailu/aat_lib/service/tracker/TrackLogger.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,15 @@ class TrackLogger(val sdirectory: SolidDataDirectory, private val presetIndex: I
6161
if (node is GpxPointNode) {
6262
setVisibleTrackPoint(node)
6363
}
64-
writer.flushOutput()
64+
writer.writeNewPoints()
65+
}
66+
67+
/**
68+
* Flush internal buffers to the output file.
69+
*/
70+
@Throws(IOException::class)
71+
override fun flush() {
72+
writer.flush()
6573
}
6674

6775
override fun close() {

aat-lib/src/test/kotlin/ch/bailu/aat_lib/file/xml/writer/GpxListWriterTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class GpxListWriterTest {
2626
val linesWritten = mockFoc.getLines()
2727

2828
// Should contain everything
29-
assertEquals(7023, linesWritten.size)
29+
assertEquals(7024, linesWritten.size)
3030

3131
val reader2 = GpxListReaderXml(mockFoc, AutoPause.NULL)
3232
assertEquals(1855, reader2.gpxList.pointList.size())

0 commit comments

Comments
 (0)