forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJPersistence.php
More file actions
52 lines (46 loc) · 1.47 KB
/
JPersistence.php
File metadata and controls
52 lines (46 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
/**
* The JPersistenceAdapter makes it possible to serialize java values.
*
* Example:
* $v=new JPersistenceAdapter(new java("java.lang.StringBuffer", "hello"));
* $id=serialize($v);
* $file=fopen("file.out","w");
* fwrite($file, $id);
* fclose($file);
*/
class JPersistenceProxy {
var $java;
var $serialID;
function __construct($java){
$this->java=$java;
$this->serialID;
}
function __sleep() {
$buf = new java("java.io.ByteArrayOutputStream");
$out = new java("java.io.ObjectOutputStream", $buf);
$out->writeObject($this->java);
$out->close();
$this->serialID = base64_encode(java_cast($buf->toByteArray(),"S"));
return array("serialID");
}
function __wakeup() {
$buf = new java("java.io.ByteArrayInputStream", base64_decode($this->serialID));
$in = new java("java.io.ObjectInputStream", $buf);
$this->java = $in->readObject();
$in->close();
}
function getJava() {
return $this->java;
}
function __destruct() {
if($this->java) return $this->java->__destruct();
}
}
class JPersistenceAdapter extends JPersistenceProxy {
function __get($arg) { if(!is_null($this->java)) return $this->java->__get($arg); }
function __set($key, $val) { if(!is_null($this->java)) return $this->java->__set($key, $val); }
function __call($m, $a) { if(!is_null($this->java)) return $this->java->__call($m,$a); }
function __toString() { if(!is_null($this->java)) return $this->java->__toString(); }
}
?>