1 package org.perl.inline.java ; 2 3 4 import java.util.* ; 5 import java.io.* ; 6 7 8 public class InlineJavaHandle { 9 private static final String charset = "ISO-8859-1" ; 10 11 read(Object o, int len)12 static String read(Object o, int len) throws InlineJavaException, IOException { 13 String ret = null ; 14 if (InlineJavaClass.ClassIsReadHandle(o.getClass())){ 15 if (o instanceof java.io.Reader){ 16 char buf[] = new char[len] ; 17 int rc = ((java.io.Reader)o).read(buf) ; 18 if (rc != -1){ 19 ret = new String(buf) ; 20 } 21 } 22 else { 23 byte buf[] = new byte[len] ; 24 int rc = ((java.io.InputStream)o).read(buf) ; 25 if (rc != -1){ 26 ret = new String(buf, charset) ; 27 } 28 } 29 } 30 else { 31 throw new InlineJavaException("Can't read from non-readhandle object (" + o.getClass().getName() + ")") ; 32 } 33 34 return ret ; 35 } 36 37 readLine(Object o)38 static String readLine(Object o) throws InlineJavaException, IOException { 39 String ret = null ; 40 if (InlineJavaClass.ClassIsReadHandle(o.getClass())){ 41 if (o instanceof java.io.BufferedReader){ 42 ret = ((java.io.BufferedReader)o).readLine() ; 43 } 44 else { 45 throw new InlineJavaException("Can't read line from non-buffered Reader or InputStream") ; 46 } 47 } 48 else { 49 throw new InlineJavaException("Can't read line from non-readhandle object (" + o.getClass().getName() + ")") ; 50 } 51 52 return ret ; 53 } 54 55 makeBuffered(Object o)56 static Object makeBuffered(Object o) throws InlineJavaException, IOException { 57 Object ret = null ; 58 if (InlineJavaClass.ClassIsReadHandle(o.getClass())){ 59 if (o instanceof java.io.BufferedReader){ 60 ret = (java.io.BufferedReader)o ; 61 } 62 else if (o instanceof java.io.Reader){ 63 ret = new BufferedReader((java.io.Reader)o) ; 64 } 65 else { 66 ret = new BufferedReader(new InputStreamReader((java.io.InputStream)o, charset)) ; 67 } 68 } 69 else if (InlineJavaClass.ClassIsWriteHandle(o.getClass())){ 70 if (o instanceof java.io.BufferedWriter){ 71 ret = (java.io.BufferedWriter)o ; 72 } 73 else if (o instanceof java.io.Writer){ 74 ret = new BufferedWriter((java.io.Writer)o) ; 75 } 76 else { 77 ret = new BufferedWriter(new OutputStreamWriter((java.io.OutputStream)o, charset)) ; 78 } 79 } 80 else { 81 throw new InlineJavaException("Can't make non-handle object buffered (" + o.getClass().getName() + ")") ; 82 } 83 84 return ret ; 85 } 86 87 write(Object o, String str)88 static int write(Object o, String str) throws InlineJavaException, IOException { 89 int ret = -1 ; 90 if (InlineJavaClass.ClassIsWriteHandle(o.getClass())){ 91 if (o instanceof java.io.Writer){ 92 ((java.io.Writer)o).write(str) ; 93 ret = str.length() ; 94 } 95 else { 96 byte b[] = str.getBytes(charset) ; 97 ((java.io.OutputStream)o).write(b) ; 98 ret = b.length ; 99 } 100 } 101 else { 102 throw new InlineJavaException("Can't write to non-writehandle object (" + o.getClass().getName() + ")") ; 103 } 104 105 return ret ; 106 } 107 108 close(Object o)109 static void close(Object o) throws InlineJavaException, IOException { 110 if (InlineJavaClass.ClassIsReadHandle(o.getClass())){ 111 if (o instanceof java.io.Reader){ 112 ((java.io.Reader)o).close() ; 113 } 114 else { 115 ((java.io.InputStream)o).close() ; 116 } 117 } 118 else if (InlineJavaClass.ClassIsWriteHandle(o.getClass())){ 119 if (o instanceof java.io.Writer){ 120 ((java.io.Writer)o).close() ; 121 } 122 else { 123 ((java.io.OutputStream)o).close() ; 124 } 125 } 126 else { 127 throw new InlineJavaException("Can't close non-handle object (" + o.getClass().getName() + ")") ; 128 } 129 } 130 } 131