1
1
package de.gmuth.ipp.client
2
2
3
3
/* *
4
- * Copyright (c) 2021-2024 Gerhard Muth
4
+ * Copyright (c) 2021-2025 Gerhard Muth
5
5
*/
6
6
7
7
import de.gmuth.ipp.core.IppAttributesGroup
@@ -11,9 +11,14 @@ import java.io.File
11
11
import java.io.IOException
12
12
import java.io.InputStream
13
13
import java.io.OutputStream
14
+ import java.nio.file.Files
15
+ import java.nio.file.Files.newOutputStream
16
+ import java.nio.file.Path
14
17
import java.util.logging.Level
15
18
import java.util.logging.Logger
16
19
import java.util.logging.Logger.getLogger
20
+ import kotlin.io.path.createDirectories
21
+ import kotlin.io.path.isRegularFile
17
22
18
23
@Suppress(" kotlin:S1192" )
19
24
class IppDocument (
@@ -30,6 +35,7 @@ class IppDocument(
30
35
" text/plain" -> " txt"
31
36
else -> mediaType.split(" /" )[1 ]
32
37
}
38
+
33
39
fun getDocumentFormatFilenameExtension (attributes : IppAttributesGroup ) =
34
40
getFilenameExtension(attributes.getValue(" document-format" ))
35
41
}
@@ -45,7 +51,7 @@ class IppDocument(
45
51
val name: IppString
46
52
get() = attributes.getValue(" document-name" )
47
53
48
- var file: File ? = null
54
+ var file: Path ? = null
49
55
50
56
fun readBytes () = inputStream.readBytes()
51
57
.also { logger.fine { " Read ${it.size} bytes of $this " } }
@@ -71,25 +77,25 @@ class IppDocument(
71
77
inputStream.copyTo(outputStream)
72
78
73
79
fun save (
74
- directory : File = job.printer.printerDirectory,
80
+ directory : Path = job.printer.printerDirectory,
75
81
filename : String = filename(),
76
82
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 ))
81
87
this .file = it
82
88
logger.info { " Saved $file ${if (attributes.containsKey(" document-format" )) " ($format )" else " " } " }
83
89
}
84
90
85
91
fun runtimeExecCommand (commandToHandleFile : String ) =
86
92
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() ))
88
94
89
95
override fun toString () = StringBuilder (" Document #$number " ).run {
90
96
append(" ($format ) of job #${job.id} " )
91
97
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)" )
93
99
toString()
94
100
}
95
101
0 commit comments