Skip to content

Commit

Permalink
Fix sync flag using inconsistent characters #3406
Browse files Browse the repository at this point in the history
  • Loading branch information
fritschldwg committed Dec 7, 2023
1 parent 2119df6 commit 49d5f54
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import java.io.BufferedInputStream
import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.nio.charset.StandardCharsets
import java.util.Scanner

object ProjectInputReader {
Expand All @@ -21,16 +20,14 @@ object ProjectInputReader {
fun extractProjectString(input: InputStream): String {
Thread.sleep(100)
val availableBytes = input.available()

if (availableBytes <= 0) {
return ""
}
if (!input.markSupported()) {
return extractProjectString(BufferedInputStream(input))
}

val isSyncSignalContained = isSyncSignalContained(input, availableBytes)

val isSyncSignalContained = containsSyncSignal(input)
if (isSyncSignalContained) {
val scanner = Scanner(input)
val stringBuilder = StringBuilder()
Expand All @@ -43,7 +40,7 @@ object ProjectInputReader {
}

val charBuffer = CharArray(1024)
val reader = BufferedReader(InputStreamReader(input, StandardCharsets.UTF_8))
val reader = BufferedReader(InputStreamReader(input))
val stringBuilder = StringBuilder()
while (reader.ready()) {
val bytesRead = reader.read(charBuffer)
Expand All @@ -53,9 +50,8 @@ object ProjectInputReader {
return content.replace(Regex("[\\n\\r]"), "")
}

private fun isSyncSignalContained(input: InputStream, availableBytes: Int): Boolean {

val bufferSize = minOf(availableBytes, 1024)
private fun containsSyncSignal(input: InputStream): Boolean {
val bufferSize = 1024
val buffer = ByteArray(bufferSize)
input.mark(bufferSize)
input.read(buffer, 0, bufferSize)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,12 @@ class ProjectInputReaderTest {
@Test
fun `Should discard newline characters when input contains newline characters`() {
// given
val syncFlag = PipeableParserSyncFlag.SYNC_FLAG.value
val line1 = "line1"
val line2 = "line2"
val newLine = "\n"

val inputStream = PipedInputStream()
val outputStream = PipedOutputStream(inputStream)
outputStream.write(syncFlag.toByteArray(StandardCharsets.UTF_8))
outputStream.write(line1.toByteArray(StandardCharsets.UTF_8))
outputStream.write(newLine.toByteArray(StandardCharsets.UTF_8))
outputStream.write(line2.toByteArray(StandardCharsets.UTF_8))
Expand Down Expand Up @@ -104,12 +102,10 @@ class ProjectInputReaderTest {
@Test
fun `Should return stream content when no valid project at end of stream`() {
// given
val syncFlag = PipeableParserSyncFlag.SYNC_FLAG.value
val invalidProjectData = "data\":\"data\"}"

val inputStream = PipedInputStream()
val outputStream = PipedOutputStream(inputStream)
outputStream.write(syncFlag.toByteArray(StandardCharsets.UTF_8))
outputStream.write(invalidProjectData.toByteArray(StandardCharsets.UTF_8))
outputStream.close()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package de.maibornwolff.codecharta.tools.pipeableparser

enum class PipeableParserSyncFlag(val value: String) {
SYNC_FLAG("\r\r\r\r\r")
SYNC_FLAG("\u000E\u000E\u000E\u000E\u000E\u000E\u000E\u000E\u000E\u000E\u000E\u000E")
}

0 comments on commit 49d5f54

Please sign in to comment.