generated from QuiltMC/quilt-template-mod
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add 'FileAccess' interface which will be used to parse songs
- Loading branch information
Showing
3 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
.../kitten-heart/src/main/java/net/pixaurora/kitten_heart/impl/resource/temp/FileAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
21 changes: 21 additions & 0 deletions
21
...eart/src/main/java/net/pixaurora/kitten_heart/impl/resource/temp/PermanentFileAccess.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...tten-heart/src/main/java/net/pixaurora/kitten_heart/impl/resource/temp/TempFileAcces.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |