Skip to content

Commit

Permalink
Add 'FileAccess' interface which will be used to parse songs
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixaurora committed Sep 26, 2024
1 parent 3f22515 commit 30758af
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.pixaurora.kitten_heart.impl.resource.temp;

import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;

import org.quiltmc.loader.api.QuiltLoader;

public interface FileAccess extends Closeable {
public static final Path TEMP_FILE_DIR = QuiltLoader.getCacheDir().resolve("kit_tunes").resolve("tmp");

public static FileAccess create(Path staticFile) {
return new PermanentFileAccess(staticFile);
}

public static FileAccess create(InputStream data) throws IOException {
Files.createDirectories(TEMP_FILE_DIR);

Path tempFile = Files.createTempFile(TEMP_FILE_DIR, null, null);

try (BufferedWriter writer = Files.newBufferedWriter(tempFile)) {
int nextByte = data.read();
while (nextByte != -1) {
writer.write((byte) nextByte);
}
}

return new TempFileAcces(tempFile);
}

public Path path();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package net.pixaurora.kitten_heart.impl.resource.temp;

import java.io.IOException;
import java.nio.file.Path;

public class PermanentFileAccess implements FileAccess {
private final Path file;

public PermanentFileAccess(Path file) {
this.file = file;
}

@Override
public void close() throws IOException {
}

@Override
public Path path() {
return this.file;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.pixaurora.kitten_heart.impl.resource.temp;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class TempFileAcces implements FileAccess {
private final Path file;

public TempFileAcces(Path file) {
this.file = file;
}

@Override
public void close() throws IOException {
Files.delete(file);
}

@Override
public Path path() {
return this.file;
}
}

0 comments on commit 30758af

Please sign in to comment.