Skip to content

Commit

Permalink
Merge pull request #6 from Displee/dev
Browse files Browse the repository at this point in the history
Addded a few I/O methods.
  • Loading branch information
Displee authored Aug 20, 2018
2 parents c8fe0b1 + eac525f commit 4ba2290
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 28 deletions.
33 changes: 9 additions & 24 deletions src/org/displee/CacheLibrary.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Arrays;

Expand All @@ -19,11 +20,6 @@
*/
public class CacheLibrary {

/**
* The revision of this cache library
*/
private final int revision;

/**
* An array of indices of this cache.
*/
Expand Down Expand Up @@ -58,26 +54,23 @@ public class CacheLibrary {
* Initialize this cache library.
* @param path The path to the cache files.
* @param mode The cache library mode.
* @param revision The revision of the cache files.
* @throws Exception
* @throws IOException If it failed to read the cache files.
*/
public CacheLibrary(String path, CacheLibraryMode mode, int revision) throws Exception {
this(path, mode, revision, null);
public CacheLibrary(String path, CacheLibraryMode mode) throws IOException {
this(path, mode, null);
}

/**
* Initialize this cache library.
* @param path The path to the cache files.
* @param mode The cache library mode.
* @param revision The revision of the cache files.
* @param listener The progress listener.
* @throws Exception
* @throws IOException If it failed to read the cache files.
*/
public CacheLibrary(String path, CacheLibraryMode mode, int revision, ProgressListener listener) throws Exception {
public CacheLibrary(String path, CacheLibraryMode mode, ProgressListener listener) throws IOException {
if (path == null) {
throw new FileNotFoundException("The path to the cache is incorrect.");
}
this.revision = revision;
this.path = path;
this.mode = mode;
final File main = new File(path + "main_file_cache.dat2");
Expand Down Expand Up @@ -162,9 +155,9 @@ public void removeLastIndex() {
checksumTable.getRandomAccessFile().setLength(id * Constants.INDEX_SIZE);
int offset = 0;
final Index[] newIndices = new Index[id];
for(int i = 0; i < indices.length; i++) {
if (indices[i].getId() != id) {
newIndices[offset++] = indices[i];
for (Index index : indices) {
if (index.getId() != id) {
newIndices[offset++] = index;
}
}
indices = newIndices;
Expand Down Expand Up @@ -247,14 +240,6 @@ public ChecksumTable getChecksumTable() {
return checksumTable;
}

/**
* Get the revision of this cache library.
* @return {@code revision}
*/
public int getRevision() {
return revision;
}

/**
* Get the path.
* @return {@code path}
Expand Down
6 changes: 3 additions & 3 deletions src/org/displee/cache/index/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public ArchiveInformation getArchiveInformation(int id) {
synchronized (super.origin.getMainFile()) {
try {
int type = 0;
if (origin.getRevision() >= 670 && id > 65535) {
if (id > 65535) {
type = 1;
}
if (super.origin.getMainFile().length() < (Constants.INDEX_SIZE * id + Constants.INDEX_SIZE)) {
Expand Down Expand Up @@ -264,7 +264,7 @@ public boolean writeArchiveInformation(int id, byte[] data) {
int chunk = 0;
int archiveDataSize = Constants.ARCHIVE_DATA_SIZE;
int archiveHeaderSize = Constants.ARCHIVE_HEADER_SIZE;
if (super.origin.getRevision() >= 670 && id > 65535) {
if (id > 65535) {
archiveDataSize -= 2;
archiveHeaderSize += 2;
}
Expand Down Expand Up @@ -337,7 +337,7 @@ public boolean cache(int[][] xteas) {
if (!cached) {
for (final int archive : super.archiveIds) {
try {
super.getArchive(archive, xteas == null ? null : xteas[archive] == null ? null : xteas[archive]);
super.getArchive(archive, xteas == null ? null : xteas[archive]);
} catch(Throwable t) {
t.printStackTrace();
}
Expand Down
18 changes: 18 additions & 0 deletions src/org/displee/io/Stream.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.displee.io;

import java.util.Arrays;

/**
* An extendible class I/O streaming serving as a base for both I/O stream.
* @author Apache Ah64
Expand Down Expand Up @@ -83,6 +85,14 @@ public int readByte() {
return getRemaining() > 0 ? buffer[offset++] : 0;
}

/**
* Read an unsigned byte.
* @return The unsigned byte.
*/
public int readUnsignedByte() {
return readByte() & 0xFF;
}

public int smf_gvlength() {
int i_20_ = readByte();
int i_21_ = 0;
Expand Down Expand Up @@ -118,6 +128,14 @@ public void writeByte(byte b, int index) {
buffer[index] = b;
}

/**
* Write a boolean.
* @param bool The condition.
*/
public void writeBoolean(boolean bool) {
writeByte(bool ? 1 : 0);
}

/**
* Expend the capacity.
*/
Expand Down
21 changes: 20 additions & 1 deletion src/org/displee/io/impl/InputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,15 @@ public int readUnsignedSmart() {
return (readShort() & 0xFFFF) - 49152;
}

/**
* Reads a smart value from the buffer (supports -1).
*
* @return the read smart value.
*/
public int readSmartNS() {
return readSmart() - 1;
}

/**
* Read a signed smart.
* @return The smart value.
Expand All @@ -123,7 +132,7 @@ public int readSmart() {

public int readBigSmart(boolean old) {
if (old) {
return readShort() & 0xFFFF;
return readUnsignedShort();
} else {
return readBigSmart();
}
Expand Down Expand Up @@ -156,6 +165,16 @@ public int readShort() {
return s;
}

/**
* Read a short unsigned.
* @return The unsigned short.
*/
public int readUnsignedShort() {
/*int i = readUnsignedByte();
return readUnsignedByte() + (i << 8);*/
return readShort() & 0xFFFF;
}

/**
* Read a short LE.
* @return The short.
Expand Down
16 changes: 16 additions & 0 deletions src/org/displee/io/impl/OutputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ public void writeSmart2(int i) {
writeSmart(i);
}

/**
* Writes an unsigned smart value to the buffer.
* @param value The value to write.
*/
public void writeUnsignedSmart(int value) {
if (value < 64 && value >= -64) {
writeByte(value + 64);
return;
}
if (value < 16384 && value >= -16384) {
writeShort(value + 49152);
} else {
System.err.println("Error psmart out of range: " + value);
}
}

/**
* Write a big smart.
* @param i
Expand Down

0 comments on commit 4ba2290

Please sign in to comment.