[libinline-java-perl] 349/398: ok

Jonas Smedegaard dr at jones.dk
Thu Feb 26 11:43:21 UTC 2015


This is an automated email from the git hooks/post-receive script.

js pushed a commit to tag 0.55
in repository libinline-java-perl.

commit 815496d052e156e302d3d9575a0c9884aee23e77
Author: patrick_leb <>
Date:   Sun Sep 4 15:05:34 2005 +0000

    ok
---
 Java/Handle.pm                                     | 265 +++++++++++++++++++++
 .../org/perl/inline/java/InlineJavaHandle.java     | 130 ++++++++++
 t/13_handles.t                                     |  79 ++++++
 t/t13.txt                                          |  10 +
 4 files changed, 484 insertions(+)

diff --git a/Java/Handle.pm b/Java/Handle.pm
new file mode 100644
index 0000000..eaf2d7a
--- /dev/null
+++ b/Java/Handle.pm
@@ -0,0 +1,265 @@
+package Inline::Java::Handle ;
+ at Inline::Java::Handle::ISA = qw(Inline::Java::Handle::Tie) ;
+
+use strict ;
+use Symbol ;
+use Carp ;
+
+$Inline::Java::Handle::VERSION = '0.50_90' ;
+
+
+# Here we store as keys the knots and as values our blessed objects
+my $OBJECTS = {} ;
+
+
+sub new {
+	my $class = shift ;
+	my $object = shift ;
+
+	my $fh = gensym() ;
+	my $knot = tie *{$fh}, $class ;
+	my $this = bless ($fh, $class) ;
+
+	$OBJECTS->{$knot} = $object ;
+
+	Inline::Java::debug(5, "this = '$this'") ; 
+	Inline::Java::debug(5, "knot = '$knot'") ;
+
+	return $this ;
+}
+
+
+sub __get_object {
+	my $this = shift ;
+
+	my $knot = tied $this || $this ;
+
+	my $ref = $OBJECTS->{$knot} ;
+	if (! defined($ref)){
+		croak "Unknown Java handle reference '$knot'" ;
+	}
+	
+	return $ref ;
+}
+
+
+sub __isa {
+	my $this = shift ;
+	my $proto = shift ;
+
+	return $this->__get_object()->__isa($proto) ;
+}
+
+
+sub __read {
+ 	my $this = shift ;
+	my ($buf, $len, $offset) = @_ ;
+
+	my $obj = $this->__get_object() ;
+
+	my $ret = undef  ;
+	eval {
+		my $str = $obj->__get_private()->{proto}->ReadFromJavaHandle($len) ;
+		$len = length($str) ;
+        if ($len > 0){
+            substr($buf, $offset, $len) = $str ;
+            $_[0] = $buf ;
+			$ret = $len ;
+        }
+	} ;
+	croak $@ if $@ ;
+
+	return $ret ;
+}
+
+
+sub __readline {
+ 	my $this = shift ;
+
+	my $obj = $this->__get_object() ;
+
+	my $ret = undef  ;
+	eval {
+		$ret = $obj->__get_private()->{proto}->ReadLineFromJavaHandle() ;
+	} ;
+	croak $@ if $@ ;
+
+	return $ret ;
+}
+
+
+sub __write {
+ 	my $this = shift ;
+	my $buf = shift ;
+	my $len = shift ;
+	my $offset = shift ;
+
+	my $obj = $this->__get_object() ;
+
+	my $ret = -1 ;
+	eval {
+		my $len = $obj->__get_private()->{proto}->WriteToJavaHandle(substr($buf, $offset, $len)) ;
+		$ret = $len ;
+	} ;
+	croak $@ if $@ ;
+}
+
+
+sub __eof {
+ 	my $this = shift ;
+}
+
+
+sub __close {
+ 	my $this = shift ;
+
+	my $obj = $this->__get_object() ;
+
+	my $ret = undef  ;
+	eval {
+		$ret = $obj->__get_private()->{proto}->CloseJavaHandle() ;
+		$obj->__get_private()->{closed} = 1 ;
+	} ;
+	croak $@ if $@ ;
+
+	return $ret ;
+}
+
+
+
+sub AUTOLOAD {
+	my $this = shift ;
+	my @args = @_ ;
+
+	use vars qw($AUTOLOAD) ;
+	my $func_name = $AUTOLOAD ;
+	# Strip package from $func_name, Java will take of finding the correct
+	# method.
+	$func_name =~ s/^(.*)::// ;
+
+	croak "Can't call method '$func_name' on Java handles" ;
+}
+
+
+sub DESTROY {
+	my $this = shift ;
+
+	my $obj = $this->__get_object() ;
+	if (! $obj->__get_private()->{closed}){
+		$this->__close() ;	
+	}
+
+	my $knot = tied *{$this} ;
+	if (! $knot){
+		Inline::Java::debug(4, "destroying Inline::Java::Handle::Tie") ;
+
+		$OBJECTS->{$this} = undef ;
+	}
+	else{
+		Inline::Java::debug(4, "destroying Inline::Java::Handle") ;
+	}
+}
+
+
+
+######################## Handle methods ########################
+package Inline::Java::Handle::Tie ;
+ at Inline::Java::Handle::Tie::ISA = qw(Tie::StdHandle) ;
+
+
+use Tie::Handle ;
+use Carp ;
+
+
+sub TIEHANDLE {
+	my $class = shift ;
+	my $jclass = shift ;
+
+	return $class->SUPER::TIEHANDLE(@_) ;
+}
+
+
+sub READ {
+ 	my $this = shift ;
+	my ($buf, $len, $offset) = @_ ;
+
+	my $ret = $this->__read($buf, $len, $offset) ;
+	$_[0] = $buf ;
+
+	return $ret ;
+}
+
+
+sub READLINE {
+ 	my $this = shift ;
+
+	return $this->__readline() ;
+}
+
+
+sub WRITE {
+ 	my $this = shift ;
+	my $buf = shift ;
+	my $len = shift ;
+	my $offset = shift ;
+
+	return $this->__write($buf, $len, $offset) ;
+}
+
+
+sub BINMODE {
+ 	my $this = shift ;
+
+	croak "Operation BINMODE not supported on Java handle" ;
+}
+
+
+sub OPEN {
+ 	my $this = shift ;
+
+	croak "Operation OPEN not supported on Java handle" ;
+}
+
+
+sub TELL {
+ 	my $this = shift ;
+
+	croak "Operation TELL not supported on Java handle" ;
+}
+
+
+sub FILENO {
+ 	my $this = shift ;
+
+	croak "Operation FILENO not supported on Java handle" ;
+}
+
+
+sub SEEK {
+ 	my $this = shift ;
+
+	croak "Operation SEEK not supported on Java handle" ;
+}
+
+
+sub EOF {
+ 	my $this = shift ;
+
+	return $this->__eof() ;
+}
+
+
+sub CLOSE {
+ 	my $this = shift ;
+		
+	return $this->__close() ;
+}
+
+
+sub DESTROY {
+ 	my $this = shift ;
+}
+
+
+1 ;
+
diff --git a/Java/sources/org/perl/inline/java/InlineJavaHandle.java b/Java/sources/org/perl/inline/java/InlineJavaHandle.java
new file mode 100644
index 0000000..63ec973
--- /dev/null
+++ b/Java/sources/org/perl/inline/java/InlineJavaHandle.java
@@ -0,0 +1,130 @@
+package org.perl.inline.java ;
+
+
+import java.util.* ;
+import java.io.* ;
+
+
+public class InlineJavaHandle {
+	private static String charset = "ISO-8859-1" ;
+
+
+	static String read(Object o, int len) throws InlineJavaException, IOException {
+		String ret = null ;
+		if (InlineJavaClass.ClassIsReadHandle(o.getClass())){
+			if (o instanceof java.io.Reader){
+				char buf[] = new char[len] ;
+				int rc = ((java.io.Reader)o).read(buf) ;
+				if (rc != -1){
+					ret = new String(buf) ;
+				}
+			}
+			else {
+				byte buf[] = new byte[len] ;
+				int rc = ((java.io.InputStream)o).read(buf) ;
+				if (rc != -1){
+					ret = new String(buf, charset) ;
+				}
+			}
+		}
+		else {
+			throw new InlineJavaException("Can't read from non-readhandle object (" + o.getClass().getName() + ")") ;
+		}
+
+		return ret ;
+	}
+
+	
+	static String readLine(Object o) throws InlineJavaException, IOException {
+		String ret = null ;
+		if (InlineJavaClass.ClassIsReadHandle(o.getClass())){
+			if (o instanceof java.io.BufferedReader){
+				ret = ((java.io.BufferedReader)o).readLine() ;
+			}
+			else {
+				throw new InlineJavaException("Can't read line from non-buffered Reader or InputStream") ;
+			}
+		}
+		else {
+			throw new InlineJavaException("Can't read line from non-readhandle object (" + o.getClass().getName() + ")") ;
+		}
+
+		return ret ;
+	}
+
+
+	static Object makeBuffered(Object o) throws InlineJavaException, IOException {
+		Object ret = null ;
+		if (InlineJavaClass.ClassIsReadHandle(o.getClass())){
+			if (o instanceof java.io.BufferedReader){
+				ret = (java.io.BufferedReader)o ;
+			}
+			else if (o instanceof java.io.Reader){
+				ret = new BufferedReader((java.io.Reader)o) ;
+			}
+			else {
+				ret = new BufferedReader(new InputStreamReader((java.io.InputStream)o, charset)) ;
+			}
+		}
+		else if (InlineJavaClass.ClassIsWriteHandle(o.getClass())){
+			if (o instanceof java.io.BufferedWriter){
+				ret = (java.io.BufferedWriter)o ;
+			}
+			else if (o instanceof java.io.Writer){
+				ret = new BufferedWriter((java.io.Writer)o) ;
+			}
+			else {
+				ret = new BufferedWriter(new OutputStreamWriter((java.io.OutputStream)o, charset)) ;
+			}
+		}
+		else {
+			throw new InlineJavaException("Can't make non-handle object buffered (" + o.getClass().getName() + ")") ;
+		}
+
+		return ret ;
+	}
+
+	
+	static int write(Object o, String str) throws InlineJavaException, IOException {
+		int ret = -1 ;
+		if (InlineJavaClass.ClassIsWriteHandle(o.getClass())){
+			if (o instanceof java.io.Writer){
+				((java.io.Writer)o).write(str) ;
+				ret = str.length() ;
+			}
+			else {
+				byte b[] = str.getBytes(charset) ;
+				((java.io.OutputStream)o).write(b) ;
+				ret = b.length ;
+			}
+		}
+		else {
+			throw new InlineJavaException("Can't write to non-writehandle object (" + o.getClass().getName() + ")") ;
+		}
+
+		return ret ;
+	}
+
+
+	static void close(Object o) throws InlineJavaException, IOException {
+		if (InlineJavaClass.ClassIsReadHandle(o.getClass())){
+			if (o instanceof java.io.Reader){
+				((java.io.Reader)o).close() ;
+			}
+			else {
+				((java.io.InputStream)o).close() ;
+			}
+		}
+		else if (InlineJavaClass.ClassIsWriteHandle(o.getClass())){
+			if (o instanceof java.io.Writer){
+				((java.io.Writer)o).close() ;
+			}
+			else {
+				((java.io.OutputStream)o).close() ;
+			}
+		}
+		else {
+			throw new InlineJavaException("Can't close non-handle object (" + o.getClass().getName() + ")") ;
+		}
+	}
+}
diff --git a/t/13_handles.t b/t/13_handles.t
new file mode 100755
index 0000000..8eda65f
--- /dev/null
+++ b/t/13_handles.t
@@ -0,0 +1,79 @@
+use strict ;
+use Test ;
+
+use Inline Config => 
+           DIRECTORY => './_Inline_test';
+
+use Inline(
+	Java => 'DATA',
+) ;
+
+use Inline::Java qw(caught) ;
+
+
+BEGIN {
+	# Leave previous server enough time to die...
+	sleep(1) ;
+	plan(tests => 2) ;
+}
+
+
+my $t = new t13() ;
+
+{
+	my $o = t13->getWriter(File::Spec->catfile("t", "t13.txt")) ;
+	my $h = new Inline::Java::Handle($o) ;
+	for (my $i = 1 ; $i <= 10 ; $i++){
+		print $h "$i\n" ;
+	}
+	close($h) ;	
+	ok(1) ;
+}
+
+ok($t->__get_private()->{proto}->ObjectCount(), 1) ;
+
+
+__END__
+
+__Java__
+
+
+import java.io.* ;
+
+class t13 {
+	public t13(){
+	}
+
+	public static Reader getReader(String file) throws FileNotFoundException {
+		return new FileReader(file) ;
+	}
+
+	public static Reader getBufferedReader(String file) throws FileNotFoundException {
+		return new BufferedReader(new FileReader(file)) ;
+	}
+
+	public static InputStream getInputStream(String file) throws FileNotFoundException {
+		return new FileInputStream(file) ;
+	}
+
+	public static InputStream getBufferedInputStream(String file) throws FileNotFoundException {
+		return new BufferedInputStream(new FileInputStream(file)) ;
+	}
+
+	public static Writer getWriter(String file) throws IOException {
+		return new FileWriter(file) ;
+	}
+
+	public static Writer getBufferedWriter(String file) throws IOException {
+		return new BufferedWriter(new FileWriter(file)) ;
+	}
+
+	public static OutputStream getOutputStream(String file) throws FileNotFoundException {
+		return new FileOutputStream(file) ;
+	}
+
+	public static OutputStream getBufferedOutputStream(String file) throws FileNotFoundException {
+		return new BufferedOutputStream(new FileOutputStream(file)) ;
+	}
+}
+
diff --git a/t/t13.txt b/t/t13.txt
new file mode 100644
index 0000000..f00c965
--- /dev/null
+++ b/t/t13.txt
@@ -0,0 +1,10 @@
+1
+2
+3
+4
+5
+6
+7
+8
+9
+10

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-perl/packages/libinline-java-perl.git



More information about the Pkg-perl-cvs-commits mailing list