Skip to content
This repository has been archived by the owner on Jun 13, 2021. It is now read-only.

Commit

Permalink
@ School fsplitinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
teddyxlandlee committed Dec 29, 2020
1 parent 8c31537 commit d08cae7
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 34 deletions.
1 change: 1 addition & 0 deletions FileSpliterator.iws
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<option value="Kotlin Class" />
<option value="Class" />
<option value="Kotlin File" />
<option value="Kotlin Data Class" />
</list>
</option>
</component>
Expand Down
60 changes: 26 additions & 34 deletions src/io/github/teddyxlandlee/nios/filesplit/Core.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package io.github.teddyxlandlee.nios.filesplit;

import io.github.teddyxlandlee.nios.filesplit.util.InvalidFileException;
import io.github.teddyxlandlee.nios.filesplit.util.NBytesHelper;
import io.github.teddyxlandlee.nios.filesplit.util.NetworkHelperKt;
import io.github.teddyxlandlee.nios.filesplit.util.*;

import java.io.*;
import java.net.MalformedURLException;
Expand Down Expand Up @@ -66,35 +64,12 @@ public static void encode(File file, int maxOneFileSize, String outputDirectory)
public static void decode(File file) {
System.out.println("Try decoding...");
String directoryWithSlash = file.getAbsolutePath() + '/';
byte[] cache;
byte[] cache = new byte[4];
int iCache;
int maxFilenameCount;
try {
FileInputStream inputStream = new FileInputStream(directoryWithSlash + "INFO.fsplitinfo");
cache = new byte[4];
iCache = inputStream.read(cache, 0, 4);
if (iCache != 4 || toInt(cache) != VersionKt.fsplitinfoHeader)
throw new InvalidFileException("INFO.fsplitinfo", 0x00000002);

iCache = inputStream.read(); // Data Version
if (iCache < 1)
throw new InvalidFileException("INFO.fsplitinfo", 0x00000003);

iCache = inputStream.read(cache, 0, 4);
if (iCache != 4)
throw new InvalidFileException("INFO.fsplitinfo", 0x00000006);
maxFilenameCount = toInt(cache);

iCache = inputStream.read(cache, 0, 4); // cache: encoded byte-string length
if (iCache != 4)
throw new InvalidFileException("INFO.fsplitinfo", 0x00000004);
iCache = toInt(cache);
cache = new byte[iCache];
iCache = inputStream.read(cache, 0, iCache);
if (iCache < 0)
throw new InvalidFileException("INFO.fsplitinfo", 0x00000005);
inputStream.close();
String newFileName = new String(cache, StandardCharsets.UTF_8);
Fsplitinfo fsplitinfo = ByteFileExecutorKt.infoFromStream(inputStream);
String newFileName = fsplitinfo.getFilename();
String newFilePath = file.getParent() + '/' + newFileName;
File newFile = new File(newFilePath);
if (newFile.exists())
Expand All @@ -105,7 +80,7 @@ public static void decode(File file) {
FileOutputStream outputStream = new FileOutputStream(newFile);

iCache = 0; // current file name count
for (int iCache2; iCache < maxFilenameCount; ++iCache) {
for (int iCache2; iCache < fsplitinfo.getMaxFileNameCount(); ++iCache) {
inputStream = new FileInputStream(file.getName() + '/' + iCache + ".fsplit");
iCache2 = inputStream.read(cache, 0, 4);
if (iCache2 != 4 || toInt(cache) != VersionKt.fsplitHeader) {
Expand All @@ -125,16 +100,33 @@ public static void decodeGit(String server, String repo, String branch, String p
try {
URL url = new URL(String.format("%s/%s/raw/%s/%s", server, repo, branch, path));
decodeGit(url);
} catch (MalformedURLException e) {
} catch (IOException e) {
e.printStackTrace();
}
}

public static void decodeGit(URL url) throws MalformedURLException {
public static void decodeGit(URL url) throws IOException {
URL urlInfo = new URL(url.toString() + "/INFO.fsplitinfo");
//File infoFile = NetworkHelperKt.tmpFileDownloaded(urlInfo);
InputStream inputStream = NetworkHelperKt.httpInputStream(urlInfo);


byte[] cache = new byte[4];

Fsplitinfo fsplitinfo = ByteFileExecutorKt.infoFromStream(inputStream);
String newFilename = fsplitinfo.getFilename();
File newFile = new File(newFilename);
if (newFile.exists())
throw new InvalidFileException(newFilename, 0x00000007);
if (!newFile.createNewFile())
throw new InvalidFileException(newFilename, 0x00000007);

FileOutputStream outputStream = new FileOutputStream(newFile);

int filenameCount = 0;
for (int iCache2; filenameCount < fsplitinfo.getMaxFileNameCount(); ++filenameCount) {
urlInfo = new URL(url.toString() + '/' + filenameCount + ".fsplit");
inputStream = NetworkHelperKt.httpInputStream(urlInfo);
iCache2 = inputStream.read(cache, 0, 4);
if (iCache2 ! = 4)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,35 @@
package io.github.teddyxlandlee.nios.filesplit.util

import io.github.teddyxlandlee.nios.filesplit.fsplitinfoHeader
import java.io.InputStream
import java.nio.charset.StandardCharsets

fun infoFromStream(inputStream: InputStream) : Fsplitinfo {
var iCache: Int
val maxFilenameCount: Int
var cache = ByteArray(4)

iCache = inputStream.read(cache, 0, 4)
if (iCache != 4 || toInt(cache) != fsplitinfoHeader)
throw InvalidFileException("INFO.fsplitinfo", 0x00000002)

iCache = inputStream.read()
if (iCache < 1)
throw InvalidFileException("INFO.fsplitinfo", 0x00000003)

iCache = inputStream.read(cache, 0, 4)
if (iCache != 4)
throw InvalidFileException("INFO.fsplitinfo", 0x00000006)
maxFilenameCount = toInt(cache)

iCache = inputStream.read(cache, 0, 4)
if (iCache != 4)
throw InvalidFileException("INFO.fsplitinfo", 0x00000004)
iCache = toInt(cache)
cache = ByteArray(4)
iCache = inputStream.read(cache, 0, iCache)
if (iCache < 0)
throw InvalidFileException("INFO.fsplitinfo", 0x00000005)
inputStream.close()
return Fsplitinfo(maxFilenameCount, String(cache, StandardCharsets.UTF_8))
}
3 changes: 3 additions & 0 deletions src/io/github/teddyxlandlee/nios/filesplit/util/Fsplitinfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package io.github.teddyxlandlee.nios.filesplit.util

data class Fsplitinfo(val maxFileNameCount: Int, val filename: String)

0 comments on commit d08cae7

Please sign in to comment.