1 28 29 package com.caucho.es; 30 31 import com.caucho.vfs.WriteStream; 32 33 import java.io.IOException ; 34 35 38 class NativeFile extends Native { 39 static ESId IN = ESId.intern("in"); 40 static ESId OUT = ESId.intern("out"); 41 42 static final int WRITE = 2; 43 static final int WRITELN = 3; 44 static final int FLUSH = 4; 45 static final int CLOSE = 5; 46 47 private NativeFile(String name, int n, int len) 48 { 49 super(name, len); 50 51 this.n = n; 52 } 53 54 static void create(Global resin) 55 { 56 put(resin, "write", WRITE, 0, DONT_ENUM); 57 put(resin, "writeln", WRITELN, 0, DONT_ENUM); 58 put(resin, "flush", FLUSH, 0, DONT_ENUM); 59 put(resin, "close", CLOSE, 0, DONT_ENUM); 60 } 61 62 private static void put(Global resin, String name, int n, int len, 63 int flags) 64 { 65 ESId id = ESId.intern(name); 66 67 resin.addProperty(id, new NativeFile(name, n, len)); 68 } 69 70 public ESBase call(Call eval, int length) throws Throwable 71 { 72 ESBase evalThis = eval.getArg(-1); 73 WriteStream stream = null; 74 75 try { 76 stream = (WriteStream) evalThis.toJavaObject(); 77 } catch (Exception e) { 78 } 79 80 if (stream == null) { 81 ESBase out = evalThis.hasProperty(OUT); 82 83 if (out != null) { 84 eval.setThis(out); 85 return out.call(eval, length, id); 86 } 87 } 88 89 switch (n) { 90 case WRITE: 91 return write(eval, length); 92 93 case WRITELN: 94 return writeln(eval, length); 95 96 case FLUSH: 97 return flush(eval, length); 98 99 case CLOSE: 100 return close(eval, length); 101 102 default: 103 throw new ESException("Unknown file function"); 104 } 105 } 106 107 private static WriteStream getWriteStream(Call eval) throws Throwable 108 { 109 ESBase evalThis = eval.getArg(-1); 110 WriteStream stream = null; 111 112 try { 113 stream = (WriteStream) evalThis.toJavaObject(); 114 } catch (Exception e) { 115 } 116 117 if (stream != null) 118 return stream; 119 120 ESBase obj = evalThis.hasProperty(OUT); 121 122 try { 123 stream = (WriteStream) obj.toJavaObject(); 124 } catch (Exception e) { 125 } 126 127 obj = Global.getGlobalProto().getGlobal().hasProperty(OUT); 128 129 try { 130 stream = (WriteStream) obj.toJavaObject(); 131 } catch (Exception e) { 132 } 133 134 if (stream == null) 135 throw new ESException("expected file as `this' or as value of `" + OUT + "'"); 136 137 return stream; 138 } 139 140 static public ESBase write(Call eval, int length) throws Throwable 141 { 142 WriteStream stream = getWriteStream(eval); 143 144 try { 145 for (int i = 0; i < length; i++) 146 stream.print(eval.getArg(i).toString()); 147 } catch (IOException e) { 148 return ESBoolean.FALSE; 149 } 150 151 return eval.getArg(-1); 152 } 153 154 static public ESBase writeln(Call eval, int length) throws Throwable 155 { 156 WriteStream stream = getWriteStream(eval); 157 158 try { 159 for (int i = 0; i < length; i++) 160 stream.print(eval.getArg(i).toString()); 161 stream.println(); 162 } catch (IOException e) { 163 return ESBoolean.FALSE; 164 } 165 166 return eval.getArg(-1); 167 } 168 169 static public ESBase flush(Call eval, int length) throws Throwable 170 { 171 WriteStream stream = getWriteStream(eval); 172 173 try { 174 stream.flush(); 175 } catch (IOException e) { 176 return ESBoolean.FALSE; 177 } 178 179 return eval.getArg(-1); 180 } 181 182 static public ESBase close(Call eval, int length) throws Throwable 183 { 184 WriteStream stream = getWriteStream(eval); 185 186 try { 187 stream.close(); 188 } catch (IOException e) { 189 return ESBoolean.FALSE; 190 } 191 192 return eval.getArg(-1); 193 } 194 } 195 | Popular Tags |