diff --git a/src/main/kotlin/de/gmuth/ipp/core/IppMessage.kt b/src/main/kotlin/de/gmuth/ipp/core/IppMessage.kt index 318bfb1e..85db28b1 100644 --- a/src/main/kotlin/de/gmuth/ipp/core/IppMessage.kt +++ b/src/main/kotlin/de/gmuth/ipp/core/IppMessage.kt @@ -29,7 +29,6 @@ abstract class IppMessage() { } val attributesGroups = mutableListOf() var documentInputStream: InputStream? = null - var documentInputStreamIsConsumed: Boolean = false var rawBytes: ByteArray? = null var documentBytes: ByteArray? = null @@ -76,7 +75,7 @@ abstract class IppMessage() { fun createAttributesGroup(tag: IppTag) = IppAttributesGroup(tag).apply { attributesGroups.add(this) } - fun hasDocument() = documentInputStream != null && documentInputStream!!.available() > 0 + fun hasDocument() = documentInputStream != null val attributesCharset: javaCharset get() = operationGroup.getValue("attributes-charset") @@ -165,14 +164,13 @@ abstract class IppMessage() { // ------------------------ private fun copyUnconsumedDocumentInputStream(outputStream: OutputStream) { - if (hasDocument() && documentInputStreamIsConsumed) throw IppException("documentInputStream is consumed") + if (hasDocument() && documentInputStream!!.available() == 0) throw IppException("documentInputStream is consumed") val outputStreamWithCopySupport = if (keepDocumentCopy) ByteArraySavingOutputStream(outputStream) else outputStream documentInputStream!! .copyTo(outputStreamWithCopySupport) // returns number of bytes copied .apply { logger.finer { "Consumed documentInputStreamWithUncompressingSupport: $this bytes" } } - documentInputStreamIsConsumed = true if (outputStreamWithCopySupport is ByteArraySavingOutputStream) { documentBytes = outputStreamWithCopySupport.getSavedBytes() logger.finer("Keeping ${documentBytes!!.size} document bytes") diff --git a/src/test/kotlin/de/gmuth/ipp/core/IppMessageTests.kt b/src/test/kotlin/de/gmuth/ipp/core/IppMessageTests.kt index e76bd6f9..a14a0d5a 100644 --- a/src/test/kotlin/de/gmuth/ipp/core/IppMessageTests.kt +++ b/src/test/kotlin/de/gmuth/ipp/core/IppMessageTests.kt @@ -4,6 +4,7 @@ package de.gmuth.ipp.core * Copyright (c) 2020-2024 Gerhard Muth */ +import de.gmuth.log.Logging import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.File.createTempFile @@ -17,6 +18,9 @@ class IppMessageTests { private val logger = getLogger(javaClass.name) + @BeforeTest + fun setUp() { Logging.configure() } + private val message = object : IppMessage() { override val codeDescription: String get() = "codeDescription" @@ -56,10 +60,11 @@ class IppMessageTests { } finally { tmpFile.delete() } - assertTrue(documentInputStreamIsConsumed) assertEquals(38, rawBytes!!.size) assertFailsWith { write(ByteArrayOutputStream()) + }.apply { + logger.info(toString()) } toString() // cover toString log(logger) // cover log @@ -79,8 +84,8 @@ class IppMessageTests { val tmpFile2 = createTempFile("test", null) try { IppMessage.keepDocumentCopy = true - write(tmpFile0.outputStream()) assertTrue(hasDocument()) + write(tmpFile0.outputStream(), true) saveDocumentBytes(tmpFile1) assertEquals(26, tmpFile1.length()) val ippBytes = encode(appendDocumentIfAvailable = false) // trigger saving raw bytes @@ -91,7 +96,6 @@ class IppMessageTests { tmpFile1.delete() tmpFile2.delete() } - assertTrue(documentInputStreamIsConsumed) } }