77import org .schabi .newpipe .extractor .downloader .Response ;
88import org .schabi .newpipe .extractor .exceptions .ReCaptchaException ;
99
10- import java .io .File ;
11- import java .io .FileOutputStream ;
1210import java .io .IOException ;
13- import java .io .OutputStreamWriter ;
1411import java .io .UncheckedIOException ;
15- import java .nio .charset .StandardCharsets ;
1612import java .nio .file .Files ;
17- import java .nio .file .Path ;
1813import java .nio .file .Paths ;
1914
2015import javax .annotation .Nonnull ;
@@ -54,12 +49,13 @@ class RecordingDownloader extends Downloader {
5449 */
5550 public RecordingDownloader (final String stringPath ) {
5651 this .path = stringPath ;
57- final Path path = Paths .get (stringPath );
58- final File folder = path .toFile ();
59- if (folder .exists ()) {
60- for (final File file : folder .listFiles ()) {
61- if (file .getName ().startsWith (RecordingDownloader .FILE_NAME_PREFIX )) {
62- file .delete ();
52+ final var path = Paths .get (stringPath );
53+ if (Files .exists (path )) {
54+ try (final var directoryStream = Files .newDirectoryStream (path ,
55+ entry -> entry .getFileName ().toString ()
56+ .startsWith (RecordingDownloader .FILE_NAME_PREFIX ))) {
57+ for (final var entry : directoryStream ) {
58+ Files .delete (entry );
6359 }
6460 }
6561 } else {
@@ -84,20 +80,13 @@ public Response execute(@Nonnull final Request request) throws IOException,
8480 response .latestUrl ()
8581 );
8682
87- final File outputFile = new File (
88- path + File .separator + FILE_NAME_PREFIX + index + ".json" );
83+ final var outputPath = Paths .get (path ).resolve (FILE_NAME_PREFIX + index + ".json" );
8984 index ++;
90- outputFile .createNewFile ();
91-
92- try (final OutputStreamWriter writer = new OutputStreamWriter (
93- new FileOutputStream (outputFile ), StandardCharsets .UTF_8 )) {
94-
85+ try (final var writer = Files .newBufferedWriter (outputPath )) {
9586 new GsonBuilder ()
96- .setPrettyPrinting ()
97- .create ()
98- .toJson (new TestRequestResponse (request , response ), writer );
99-
100- writer .flush ();
87+ .setPrettyPrinting ()
88+ .create ()
89+ .toJson (new TestRequestResponse (request , response ), writer );
10190 }
10291
10392 return response ;
0 commit comments