Skip to content

Commit

Permalink
writeText
Browse files Browse the repository at this point in the history
  • Loading branch information
gmuth committed Sep 26, 2024
1 parent 782feb8 commit 5ac5685
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
16 changes: 7 additions & 9 deletions src/main/kotlin/de/gmuth/ipp/core/IppAttributesGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class IppAttributesGroup(val tag: IppTag) : LinkedHashMap<String, IppAttribute<*
get(name)?.getStringValues() ?: throw IppAttributeNotFoundException(name, tag)

fun getValueAsZonedDateTime(name: String, zoneId: ZoneId = ZoneId.systemDefault()): ZonedDateTime =
get(name)?.getValueAsZonedDateTime()?.withZoneSameInstant(zoneId) ?: throw IppAttributeNotFoundException(name, tag)
get(name)?.getValueAsZonedDateTime()?.withZoneSameInstant(zoneId)
?: throw IppAttributeNotFoundException(name, tag)

fun getValueAsDurationOfSeconds(name: String): Duration =
get(name)?.getValueAsDurationOfSeconds() ?: throw IppAttributeNotFoundException(name, tag)
Expand All @@ -97,17 +98,14 @@ class IppAttributesGroup(val tag: IppTag) : LinkedHashMap<String, IppAttribute<*
keys.forEach { logger.log(level) { "$prefix ${get(it)}" } }
}

fun write(bufferedWriter: BufferedWriter, title: String = "$tag") = bufferedWriter.run {
write(title)
newLine()
values.forEach {
write(" $it")
newLine()
}
@JvmOverloads
fun writeText(bufferedWriter: BufferedWriter, title: String = "$tag") = bufferedWriter.run {
write(title); newLine()
values.forEach { write(" $it"); newLine() }
}

fun saveText(file: File) = file.apply {
bufferedWriter().use { write(it) }
bufferedWriter().use { writeText(it) }
logger.info { "Saved $path" }
}
}
16 changes: 7 additions & 9 deletions src/main/kotlin/de/gmuth/ipp/core/IppMessage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -183,21 +183,19 @@ abstract class IppMessage() {
}

@JvmOverloads
fun write(bufferedWriter: BufferedWriter, title: String? = null) {
fun writeln(text: String) = bufferedWriter.run { write(text); newLine() }
title?.also { bufferedWriter.write(it) }
if (rawBytes != null) bufferedWriter.write(" (decoded ${rawBytes!!.size} raw IPP bytes)")
bufferedWriter.newLine()
fun writeText(bufferedWriter: BufferedWriter, title: String? = null) = bufferedWriter.run {
fun BufferedWriter.writeln(text: String) { write(text); newLine() }
title?.also { write(it) }
if (rawBytes != null) write(" (decoded ${rawBytes!!.size} raw IPP bytes)")
newLine()
writeln("version $version")
writeln(codeDescription)
writeln("request-id $requestId")
for (group in attributesGroups) {
group.write(bufferedWriter)
}
attributesGroups.forEach { it.writeText(bufferedWriter) }
}

fun saveText(file: File) = file.apply {
bufferedWriter().use { write(it, title = "# File: ${file.name}") }
bufferedWriter().use { writeText(it, title = "# File: ${file.name}") }
logger.info { "Saved $path (${length()} bytes)" }
}

Expand Down

0 comments on commit 5ac5685

Please sign in to comment.