Well… this is my contribution to this forum…
My experience in java is just a week! :-X
So… try to don’t hit me too much! xD
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.util.*;
import java.util.zip.*;
public class FileSystem
{
/*
* Constants
*/
public static final int INT = 0;
public static final double DOUBLE = 1;
public static final long LONG = 2;
public static final float FLOAT = 3;
public static final short SHORT = 4;
public static final char CHAR = 5;
public static final byte BYTE = 6;
/*
* Variables
*/
private static File file = null;
private static FileInputStream file_input = null;
private static DataInputStream data_in = null;
public static file_t filesystem = null;
public static file_t work_node = null;
private static int id_counter = 0;
/*
* Start
*/
public static void Start ()
{
id_counter = 0;
filesystem = null;
filesystem = Node_Create (filesystem, "<null>", null);
work_node = filesystem;
}
/*
* -------------------------------------------------------------------------
* Node implementation
*/
/*
* Node_Create
*/
private static file_t Node_Create (file_t a, String name, ByteBuffer buffer)
{
a = new file_t ();
a.filename = name;
a.backbuffer = buffer;
a.next = null;
return a;
}
/*
* Node_Add
*/
private static file_t Node_Add (file_t a, String name, ByteBuffer buffer)
{
if (a.next == null)
{
a.next = Node_Create (a.next, name, buffer);
work_node = a.next;
}
return a;
}
/*
* Node_Find
*/
private static file_t Node_Find (file_t start, String name)
{
file_t a = start;
file_t tmp = null;
while (a != null)
{
if (a.filename.indexOf (name, 0) != -1)
{
if (a.backbuffer != null)
{
return a;
}
}
tmp = a.next;
a = tmp;
}
return null;
}
/*
* Node_ReadInt
*/
private static int Node_ReadInt (file_t fp)
{
if (fp.backbuffer == null)
{
return -1;
}
return fp.backbuffer.getInt ();
}
/*
* Node_ReadDouble
*/
private static double Node_ReadDouble (file_t fp)
{
if (fp.backbuffer == null)
{
return -1;
}
return fp.backbuffer.getDouble ();
}
/*
* Node_ReadLong
*/
private static long Node_ReadLong (file_t fp)
{
if (fp.backbuffer == null)
{
return -1;
}
return fp.backbuffer.getLong ();
}
/*
* Node_ReadFloat
*/
private static float Node_ReadFloat (file_t fp)
{
if (fp.backbuffer == null)
{
return -1;
}
return fp.backbuffer.getFloat ();
}
/*
* Node_ReadShort
*/
private static short Node_ReadShort (file_t fp)
{
if (fp.backbuffer == null)
{
return -1;
}
return fp.backbuffer.getShort ();
}
/*
* Node_ReadChar
*/
private static char Node_ReadChar (file_t fp)
{
if (fp.backbuffer == null)
{
return 0;
}
return fp.backbuffer.getChar ();
}
/*
* Node_ReadByte
*/
private static byte Node_ReadByte (file_t fp)
{
if (fp.backbuffer == null)
{
return 0;
}
return fp.backbuffer.get ();
}
/*
* -------------------------------------------------------------------------
* Public interface
*/
/*
* fadd
*/
public static boolean fadd (String name)
{
File file = null;
int fileLength = 0;
FileChannel channel = null;
FileInputStream input = null;
ByteBuffer buffer = null;
boolean ispak = false;
if (name.indexOf (Global.FS_PAK_EXTENSION, 0) != -1)
{
ispak = true;
}
try
{
if (!ispak)
{
file = new File(name);
if (file.canRead())
{
input = new FileInputStream(file);
channel = input.getChannel();
fileLength = (int) channel.size();
buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0,
fileLength);
input.close();
buffer.order (ByteOrder.LITTLE_ENDIAN);
work_node = Node_Add (work_node, name, buffer);
work_node = work_node.next;
System.out.println ("loaded >> " + name);
return true;
}
}
else
{
ZipInputStream zip_in_stream;
file = new File(name);
if (file.canRead())
{
FileInputStream in = new FileInputStream (file);
BufferedInputStream source = new BufferedInputStream (in);
zip_in_stream = new ZipInputStream (source);
while (true)
{
ZipEntry zip_entry = zip_in_stream.getNextEntry ();
if (zip_entry == null)
{
break;
}
int size = (int)zip_entry.getSize ();
byte[] input_buffer = new byte[size];
int len = 0;
String filename = zip_entry.getName ();
len = zip_in_stream.read (input_buffer, 0, size);
buffer = ByteBuffer.wrap (input_buffer);
buffer.order (ByteOrder.LITTLE_ENDIAN);
work_node = Node_Add (work_node, filename, buffer);
work_node = work_node.next;
System.out.println ("loaded >> " + zip_entry.getName ());
}
zip_in_stream.close ();
return true;
}
}
}
catch (Exception e)
{
System.out.println ("exception >> " + e);
}
return false;
}
/*
* fopen
*/
public static file_t fopen (String name)
{
// file_t ret = Node_Search (name, filesystem);
file_t ret = Node_Find (filesystem, name);
if (ret != null)
{
ret.backbuffer.position (0);
return ret;
}
return null;
}
/*
* fclose
*/
public static void fclose (file_t fp)
{
}
/*
* -------------------------------------------------------------------------
* fread
*/
/*
* int
*/
public static int fread (file_t fp, int a)
{
return Node_ReadInt (fp);
}
/*
* double
*/
public static double fread (file_t fp, double a)
{
return Node_ReadDouble (fp);
}
/*
* long
*/
public static long fread (file_t fp, long a)
{
return Node_ReadLong (fp);
}
/*
* float
*/
public static float fread (file_t fp, float a)
{
return Node_ReadFloat (fp);
}
/*
* short
*/
public static short fread (file_t fp, short a)
{
return Node_ReadShort (fp);
}
/*
* char
*/
public static char fread (file_t fp, char a)
{
return Node_ReadChar (fp);
}
/*
* byte
*/
public static byte fread (file_t fp, byte a)
{
return Node_ReadByte (fp);
}
}
Any comments or something like that will be apreciated.