Allow garbage-collecting object internal state
[jpcrr.git] / org / jpc / luaextensions / BinaryOutFile.java
blob6f79de50b363f76c9a952c6fd570298f20f6df28
1 /*
2 JPC-RR: A x86 PC Hardware Emulator
3 Release 1
5 Copyright (C) 2007-2009 Isis Innovation Limited
6 Copyright (C) 2009 H. Ilari Liusvaara
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as published by
10 the Free Software Foundation.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License along
18 with this program; if not, write to the Free Software Foundation, Inc.,
19 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 Based on JPC x86 PC Hardware emulator,
22 A project from the Physics Dept, The University of Oxford
24 Details about original JPC can be found at:
26 www-jpc.physics.ox.ac.uk
30 package org.jpc.luaextensions;
32 import mnj.lua.*;
34 import java.io.*;
35 import java.util.*;
36 import java.util.zip.*;
37 import java.lang.reflect.*;
39 import org.jpc.plugins.LuaPlugin;
40 import org.jpc.jrsr.*;
41 import static org.jpc.Misc.parseStringToComponents;
42 import static org.jpc.Misc.errorDialog;
43 import static org.jpc.Misc.tempname;
44 import static org.jpc.Misc.nextParseLine;
45 import static org.jpc.Misc.parseString;
46 import static org.jpc.Misc.encodeLine;
48 //Locking this class is used for preventing termination and when terminating.
49 public class BinaryOutFile extends LuaPlugin.LuaResource
51 OutputStream object;
53 BinaryOutFile(LuaPlugin plugin, OutputStream os) throws IOException
55 super(plugin);
56 object = os;
59 public void destroy() throws IOException
61 object.close();
62 object = null;
65 public int luaCB_four_to_five(Lua l, LuaPlugin plugin)
67 try {
68 plugin.generateLuaClass(l, new BinaryOutFile(plugin, new FourToFiveEncoder(object)));
69 } catch(IOException e) {
70 l.pushNil();
71 l.pushString("IOException: " + e.getMessage());
72 return 2;
73 } catch(IllegalArgumentException e) {
74 l.pushNil();
75 l.pushString("Illegal argument: " + e.getMessage());
76 return 2;
78 return 1;
81 public int luaCB_deflate(Lua l, LuaPlugin plugin)
83 try {
84 plugin.generateLuaClass(l, new BinaryOutFile(plugin, new DeflaterOutputStream(object)));
85 } catch(IOException e) {
86 l.pushNil();
87 l.pushString("IOException: " + e.getMessage());
88 return 2;
89 } catch(IllegalArgumentException e) {
90 l.pushNil();
91 l.pushString("Illegal argument: " + e.getMessage());
92 return 2;
94 return 1;
97 public int luaCB_text(Lua l, LuaPlugin plugin)
99 try {
100 plugin.generateLuaClass(l, new TextOutFile(plugin, object));
101 } catch(IOException e) {
102 l.pushNil();
103 l.pushString("IOException: " + e.getMessage());
104 return 2;
105 } catch(IllegalArgumentException e) {
106 l.pushNil();
107 l.pushString("Illegal argument: " + e.getMessage());
108 return 2;
110 return 1;
113 public int luaCB_write(Lua l, LuaPlugin plugin)
115 l.pushNil();
116 try {
117 String towrite = l.checkString(2);
118 if(towrite == "") {
119 l.pushBoolean(true);
120 return 1;
122 byte[] tmp = new byte[towrite.length()];
123 for(int i = 0; i < towrite.length(); i++)
124 tmp[i] = (byte)towrite.charAt(i);
125 object.write(tmp);
126 l.pushBoolean(true);
127 } catch(IOException e) {
128 l.pushBoolean(false);
129 l.pushString("IOException: " + e.getMessage());
130 return 2;
132 return 1;
135 public int luaCB_close(Lua l, LuaPlugin plugin)
137 try {
138 plugin.destroyLuaObject(l);
139 l.pushBoolean(true);
140 } catch(IOException e) {
141 l.pushBoolean(false);
142 l.pushString("IOException: " + e.getMessage());
143 return 2;
145 return 1;
148 public static int luaCB_open(Lua l, LuaPlugin plugin)
150 l.pushNil();
151 String name = l.checkString(1);
152 try {
153 plugin.generateLuaClass(l, new BinaryOutFile(plugin, new FileOutputStream(name)));
154 } catch(IOException e) {
155 l.pushNil();
156 l.pushString("IOException: " + e.getMessage());
157 return 2;
158 } catch(IllegalArgumentException e) {
159 l.pushNil();
160 l.pushString("Illegal argument: " + e.getMessage());
161 return 2;
163 return 1;