/*
CLASS {NATIVE_File_stream|_Java}, NATIVE :

*/

import java.util.ArrayList;
import java.io.*;

public class NATIVE_File_stream {
    
    
    public static ArrayList<RandomAccessFile> All_streams = 
        new ArrayList<RandomAccessFile>();
    
    /*
    // {File_stream} NATIVE functions code:
    
    CONST STR :
        NATIVE_File_stream_import = @
        "import java.io.*;"
        
		NATIVE_All_streams = ""
        
        NATIVE_File_stream_open = @
        "    if (NATIVE_File_stream.All_streams.size() == 0)"
        "       NATIVE_File_stream.All_streams.add(null);"
        "    int h = NATIVE_File_stream.All_streams.lastIndexOf(null);"
        "    if (h <= 0)"
        "    {"
        "       h = NATIVE_File_stream.All_streams.size();"
        "       NATIVE_File_stream.All_streams.add(null);"
        "    }"
        "    String mode = " #QU "r" #QU ";"
        "    if (X_Mode != 0) mode = " #QU "rw" #QU ";"
        "    try {"
        "       File f = new File(X_Path);"
        "       if (!f.isFile()) {"
        "			f.createNewFile();"
        "		}"
        "		RandomAccessFile fs = new RandomAccessFile(f, mode);"
        "       NATIVE_File_stream.All_streams.set(h, fs);"
        "		//if (X_Mode != 0) "
        "		fs.seek(0);"
        "    }"
        "    catch (Exception E) {"
        "       AL4_system.Any_exception(E);"
        "    }"
        "    RESULT = h;"

        NATIVE_File_stream_destroy = @
        "    if (X_Handle < 1 || "
        "       X_Handle >= NATIVE_File_stream.All_streams.size() ||"
        "       NATIVE_File_stream.All_streams.get(X_Handle) == null) return;"
        "    //if (!F_is_closed_stream)"
        "       NATIVE_File_stream.All_streams.get(X_Handle).close();"
        "    NATIVE_File_stream.All_streams.set(X_Handle, null);"
        //"    X_Handle = 0;"
        
        NATIVE_File_stream_size = @
        "    if (X_Handle < 1 || "
        "       X_Handle >= NATIVE_File_stream.All_streams.size() ||"
        "       NATIVE_File_stream.All_streams.get(X_Handle) == null)"
        "       { }"
        "    else {"
        "       long R = NATIVE_File_stream.All_streams.get(X_Handle).length();"
        "		X_lo_hi.Set_item(0, (int)R);"     
        "		X_lo_hi.Set_item(1, (int)(R >>> 32));"    
        "	 }"
        
        NATIVE_File_stream_set_size = @
        "    if (X_Handle < 1 || "
        "       X_Handle >= NATIVE_File_stream.All_streams.size() ||"
        "       NATIVE_File_stream.All_streams.get(X_Handle) == null)"
        "       { }"
        "    else {"
        "       long R = X_lo_hi.Get_item(0);"
        "		R = R | (X_lo_hi.Get_item(1) << 16) << 16;"
        "		NATIVE_File_stream.All_streams.get(X_Handle).setLength(R);"
        "	 }"

        
        NATIVE_File_stream_position = @
        "    if (X_Handle < 1 || "
        "       X_Handle >= NATIVE_File_stream.All_streams.size() ||"
        "       NATIVE_File_stream.All_streams.get(X_Handle) == null)"
        "       { }"
        "    else {"
        "		long R = NATIVE_File_stream.All_streams.get(X_Handle)"
        "       .getFilePointer();"
        "		X_lo_hi.Set_item(0, (int)R);"     
        "		X_lo_hi.Set_item(1, (int)(R >>> 32));"    
        "		}"     
        
        NATIVE_File_stream_set_pos = @
        "    if (X_Handle < 1 || "
        "       X_Handle >= NATIVE_File_stream.All_streams.size() ||"
        "       NATIVE_File_stream.All_streams.get(X_Handle) == null) return;"
        "    RandomAccessFile fs = "
        "       NATIVE_File_stream.All_streams.get(X_Handle);"
        "    if (fs.getFilePointer() > fs.length())"
        "        fs.setLength(fs.getFilePointer());"
        "    long To = X_New_lo | ((long)X_New_hi << 32);"
        "    switch (X_Disp)" 
        "    {"
        "       case 1: To += fs.getFilePointer(); break;"
        "       case 2: To += fs.length(); break;"
        "    }"
        "    if (To < 0) To = 0;"
        "    if (To >= fs.length()) To = fs.length();"
        "    fs.seek(To);"
        "	 fs.getFilePointer();"
        
        NATIVE_File_stream_close = @
        "    if (X_Handle < 1 || "
        "       X_Handle >= NATIVE_File_stream.All_streams.size() ||"
        "       NATIVE_File_stream.All_streams.get(X_Handle) == null) return;"
        "	 //NATIVE_File_stream.All_streams.get(X_Handle).flush();"
        "    NATIVE_File_stream.All_streams.get(X_Handle).close();"
        
        NATIVE_File_stream_read_bytes = @
        "    if (X_Handle < 1 || "
        "       X_Handle >= NATIVE_File_stream.All_streams.size() ||"
        "       NATIVE_File_stream.All_streams.get(X_Handle) == null)"
        "       return 0;"
        "    try {"
        "        byte[] read_buf = new byte[X_Dest.Count()];"
        "        RESULT = NATIVE_File_stream.All_streams.get(X_Handle).read("
        "           read_buf, 0, X_Dest.Count());"
        "        for (int i = 0; i < X_Dest.Count(); i++)"
        "            X_Dest.Set_item(i, read_buf[i]);"
        "    }"
        "    catch(Exception e) {"
        "        AL4_system.Any_exception(e);"
        "    }"
        
        NATIVE_File_stream_write_bytes = @
        "    if (X_Handle < 1 || "
        "       X_Handle >= NATIVE_File_stream.All_streams.size() ||"
        "       NATIVE_File_stream.All_streams.get(X_Handle) == null)"
        "       return 0;"
        "    try {"
        "        RandomAccessFile fs = NATIVE_File_stream.All_streams.get("
        "           X_Handle);"
        "        long pos = fs.getFilePointer();"
        "        byte[] from = X_Source.BaseList.ToArray();"
        "        fs.write(from, X_Source.Starting, X_Source.Count());"
        "        RESULT = (int)(fs.getFilePointer() - pos);"
        "    }"
        "    catch(Exception e) {"
        "        AL4_system.Any_exception(e);"
        "    }"
        
        .
    */


}
/*
END
*/

