11package de.gmuth.ipp.client
22
33/* *
4- * Copyright (c) 2021-2024 Gerhard Muth
4+ * Copyright (c) 2021-2025 Gerhard Muth
55 */
66
77import de.gmuth.ipp.core.IppAttributesGroup
@@ -11,9 +11,14 @@ import java.io.File
1111import java.io.IOException
1212import java.io.InputStream
1313import java.io.OutputStream
14+ import java.nio.file.Files
15+ import java.nio.file.Files.newOutputStream
16+ import java.nio.file.Path
1417import java.util.logging.Level
1518import java.util.logging.Logger
1619import java.util.logging.Logger.getLogger
20+ import kotlin.io.path.createDirectories
21+ import kotlin.io.path.isRegularFile
1722
1823@Suppress(" kotlin:S1192" )
1924class IppDocument (
@@ -30,6 +35,7 @@ class IppDocument(
3035 " text/plain" -> " txt"
3136 else -> mediaType.split(" /" )[1 ]
3237 }
38+
3339 fun getDocumentFormatFilenameExtension (attributes : IppAttributesGroup ) =
3440 getFilenameExtension(attributes.getValue(" document-format" ))
3541 }
@@ -45,7 +51,7 @@ class IppDocument(
4551 val name: IppString
4652 get() = attributes.getValue(" document-name" )
4753
48- var file: File ? = null
54+ var file: Path ? = null
4955
5056 fun readBytes () = inputStream.readBytes()
5157 .also { logger.fine { " Read ${it.size} bytes of $this " } }
@@ -71,25 +77,25 @@ class IppDocument(
7177 inputStream.copyTo(outputStream)
7278
7379 fun save (
74- directory : File = job.printer.printerDirectory,
80+ directory : Path = job.printer.printerDirectory,
7581 filename : String = filename(),
7682 overwrite : Boolean = true
77- ) = File ( directory, filename).also {
78- if ( ! directory.exists()) directory.mkdirs ()
79- if (it.isFile && ! overwrite) throw IOException (" File '$it ' already exists" )
80- copyTo(it.outputStream( ))
83+ ) = directory.resolve( filename).also {
84+ it.parent?.createDirectories ()
85+ if (it.isRegularFile() && ! overwrite) throw IOException (" File '$it ' already exists" )
86+ copyTo(newOutputStream(it ))
8187 this .file = it
8288 logger.info { " Saved $file ${if (attributes.containsKey(" document-format" )) " ($format )" else " " } " }
8389 }
8490
8591 fun runtimeExecCommand (commandToHandleFile : String ) =
8692 if (file == null ) throw IppException (" Missing file to handle." )
87- else Runtime .getRuntime().exec(arrayOf(commandToHandleFile, file!! .absolutePath ))
93+ else Runtime .getRuntime().exec(arrayOf(commandToHandleFile, file!! .toAbsolutePath().toString() ))
8894
8995 override fun toString () = StringBuilder (" Document #$number " ).run {
9096 append(" ($format ) of job #${job.id} " )
9197 if (attributes.containsKey(" document-name" )) append(" '$name '" )
92- if (file != null ) append(" : $file (${file!! .length( )} bytes)" )
98+ if (file != null ) append(" : $file (${Files .size( file!! )} bytes)" )
9399 toString()
94100 }
95101
0 commit comments