E/A-Ausnahme beim Leseversuch im geschlossenen Stream

Post a reply

Smilies
:) :( :oops: :chelo: :roll: :wink: :muza: :sorry: :angel: :read: *x) :clever:
View more smilies

BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON

Topic review
   

Expand view Topic review: E/A-Ausnahme beim Leseversuch im geschlossenen Stream

by Guest » 13 Jan 2025, 11:36

Ich habe eine ZIP-Datei von einem Anbieter heruntergeladen, der Download funktioniert gut und dann muss ich die ZIP-Datei verarbeiten. Die Struktur der ZIP-Datei ist wie folgt:

Code: Select all

zipFile.zip/MainFolder/MyFile.xml
Aber wenn ich versuche, die Datei zu verarbeiten, wird in meinen Protokollen dieser Fehler angezeigt:

Code: Select all

java.io.IOException: Attempted read on closed stream
Und ich verstehe nicht, was los ist, hier ist der Code, der sich mit all dem befasst:

Code: Select all

private fun downloadDataToDisk(
source: FileSource,
name: String,
fileType: FileType
): Either {

val filePath =
prepareDirectory().flatMap { _ ->
val fullFill =
when (fileType) {
FileType.DAY -> {
source.dataUrl +
"${generateDateForDownload(source.daysAgo.toLong())}${fileType.fileType}.zip"
}
FileType.FULL -> {
source.dataUrl +
"${generateDateForDownload(source.daysAgo.toLong())}${fileType.fileType}.zip"
}
}
fullFill.right()
}

val downloadedFile = filePath.flatMap { path ->
externalClient.loadDowJonesFile(path).mapLeft{ mapClientError(it) }
}

val storedFileStatus = downloadedFile.flatMap { inputStream ->
filePath.flatMap { path ->
inputStream.use { storeFile(path, name, it) }
}
}

return storedFileStatus.flatMap { filePath }
}

Code: Select all

private fun prepareDirectory(): Either {
return Either.conditionally(
File(properties.fileStoragePath).exists(),
{ directoryMissingError() },
{ properties.fileStoragePath }
)
}

Code: Select all

private fun storeFile(
path: String,
fileName: String,
input: InputStream
): Either {
val filePath =
Either.catch {
val file = File(path, fileName)
if (!file.exists()) {
file.createNewFile()
file.outputStream().use { target -> input.copyTo(target) }
}
file.path
}
.mapLeft { fileStorageError(fileName, it) }
.tap { log.atInfo().log("Successfully downloaded file $fileName") }

return filePath
}
```

Top