高分,帮忙下,谁知道把PhP代码转成Java代码。。。

tashiwoweiyi 2011-07-25 12:45:31
我不懂PhP,,,谁能麻烦下,把以下PhP代码转成Java代码。。

谢谢。。。

原代码地址


[php]
<?php
/**
*=------------------------------------------------------------------------=
* InstantRecorder.php
*=------------------------------------------------------------------------=
*
*
* Copyright(c) 2008 by guigui. All rights reserved.
* @author guigui <evan_gui@163.com>
* @version $Id: InstantRecorder.php, v 1.0 2008/4/21 $
* @package systen
* @link http://www.guigui8.com/index.php/archives/108.html
*
* history:
* 1. add this file. (by 桂桂 on 2008/4/21)
*/
define('INSTANT_DATA_SEP', '|g|'); //及时存储的数据 字段间的分隔符
define('MAX_MEM_QUEUE_LENGTH', 1000); //memcached中允许存在记录条数的最大值
/**
*=--------------------------------------------------------------------=
* class InstantRecorder
*=--------------------------------------------------------------------=
*
* 及时数据记录类 的 工场类
*
*/
class InstantRecorder
{
/**
* 获取及时数据记录的对象实例
*
* @param string $mode
* @param string $dir - 最终存储文件时,数据文件被存储的目录路径
* @param string $dataFileSurfix - 最终存储文件时,数据文件名的后缀
* @return InstantMemcachedRecorder or InstantFileRecorder object
*/
static public function getRecorder($mode, $dir = '', $dataFileSurfix = '') {
switch (trim($mode)) {
case 'mem':
return new InstantMemcachedRecorder($dir, $dataFileSurfix);
break;
case 'file':
return new InstantFileRecorder($dir, $dataFileSurfix);
break;
default:
return new InstantFileRecorder($dir, $dataFileSurfix);
}// end of switch
}//end of function

}//end of class
/**
*=--------------------------------------------------------------------=
* class InstantMemcachedRecorder
*=--------------------------------------------------------------------=
*
* 用memcache记录及时数据的处理类
*
* 注:memcache记录的数据,最终要被写到文件中去;
* 写入时机根据memcache设置的缓存队列长度而定。
* 请通过setQueueLength()方法设置队列长度。
*
*/
class InstantMemcachedRecorder
{
private $_host = "127.0.0.1"; //提供memcache服务的主机
private $_port = "11211"; //memcache使用端口号
private $_dataFileSurfix; //数据文件名的后缀
private $_baseDir; //数据文件的目录路径

private $_memcachedObj; //memcached对象句柄
private $_queueLenth; //memcached允许的某个索引对应的记录队列条数的最大值


/**
*=-------------------------------------------------------------------=
*=-------------------------------------------------------------------=
* Public Methods
*=-------------------------------------------------------------------=
*=-------------------------------------------------------------------=
*/

/**
* memcache及时数据记录的对象构造函数
*
* @param string $dir - 最终存储文件时,数据文件被存储的目录路径
* @param string $dataFileSurfix - 最终存储文件时,数据文件名的后缀
* @return InstantMemcachedRecorder object
*/
public function __construct($dir = '', $dataFileSurfix = '') {
if (empty($dir)) {
$dir = dirname(__FILE__) . DIRECTORY_SEPARATOR;
}
if (empty($dataFileSurfix)) {
$dataFileSurfix = '_noname.txt';
}
$this->setBaseDir($dir);
$this->setDataFileSurfix($dataFileSurfix);
$this->_queueLenth = MAX_MEM_QUEUE_LENGTH;
}

/**
* 设置memcache缓存队列的长度
*
* @param unknown_type $len
* @return unknown
*/
public function setQueueLength($len=0) {
$len = intval($len);
if ($len < 1) return false;
$this->_queueLenth = $len;
}


/**
* 设置数据文件的文件缀名
*
* @param unknown_type $str
*/
public function setDataFileSurfix($str) {
$this->_dataFileSurfix = $str;
}

/**
* 设置数据文件名
*
* @param unknown_type $str
*/
public function setDateFileName($str) {
$this->_dateFileName = $str;
}

/**
* 设置数据文件的目录路径
*
* @param unknown_type $dir
*/
public function setBaseDir($dir) {
$this->_baseDir = $dir;
}

/**
* 将数据进行及时记录
*
* @param string $data
* @return boolean
*/
public function record($data) {
//
// 1. detect and initiate memcache module, if not found, the then use file writing strategy.
//
if (!class_exists('Memcache') || !function_exists('memcache_connect')) {
$file_recorder = new InstantFileRecorder($this->_baseDir, $this->_dataFileSurfix);
return $file_recorder->record($data);
} else {
$this->_initMemcached();
}


//
//2. write data into memcache.
//
$data_key_prefix = trim($this->_baseDir) . trim($this->_dataFileSurfix);
$num_key = $data_key_prefix . '_num';
$cur_queue_num = intval($this->_memcachedObj->get($num_key));
$data_key = $data_key_prefix . $cur_queue_num;

$this->_memcachedObj->set($data_key, $data . "\r\n");

if ($cur_queue_num >= $this->_queueLenth - 1) {
//if the record time surplus 23:55, we write all memcached data into file.
$this->_writeIntoFile($data_key_prefix, $this->_queueLenth);
$this->_memcachedObj->set($num_key, 0);
} else {
$this->_memcachedObj->set($num_key, $cur_queue_num + 1);

//if the record time surplus 23:55, we write all memcached data into file.
if (intval(date('Hi')) >= 2355) {
$this->_writeIntoFile($data_key_prefix, $cur_queue_num + 1);
$this->_memcachedObj->set($num_key, 0);
}
}
return true;
}

/**
* 将所有通过本类处理的所有memcache数据,立即写入到文件
*
* @return unknown
*/
public function clearMemData() {
$this->_initMemcached();
$data_key_prefix = trim($this->_baseDir) . trim($this->_dataFileSurfix);
return $this->_writeIntoFile($data_key_prefix);
}


/**
*=-------------------------------------------------------------------=
*=-------------------------------------------------------------------=
* Private Methods
*=-------------------------------------------------------------------=
*=-------------------------------------------------------------------=
*/

/**
* 初始化memcache对象句柄
*
*/
private function _initMemcached() {
$this->_memcachedObj = new Memcache();
$this->_memcachedObj->connect($this->_host, $this->_port)
or die ("Could not connect memcached server!");
}

/**
* 将memcache缓存的数据 立即 写入到指定的 文件
*
* @param string $data_key_prefix - 被写入的memcache数据key的前缀名
* @param integer $flush_queue_len - 设置写入的队列长度(默认为0,表示都写入)
*/
private function _writeIntoFile($data_key_prefix, $flush_queue_len=0) {
$flush_queue_len == 0 && $flush_queue_len = $this->_queueLenth;
$file_recorder = new InstantFileRecorder($this->_baseDir, $this->_dataFileSurfix);
$data = '';
for ($i = 0; $i < $flush_queue_len; $i++) {
$data_key = $data_key_prefix . $i;
$data .= $this->_memcachedObj->get($data_key);
$this->_memcachedObj->set($data_key, '');
}
$data = substr($data, 0, -2);
$file_recorder->record($data);
}

private function _needFlush() { }

}//end of class
...全文
460 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
panzhaojl 2011-08-03
  • 打赏
  • 举报
回复
老张-AI 2011-07-25
  • 打赏
  • 举报
回复
这么多估计够呛,友情帮顶
飞跃颠峰 2011-07-25
  • 打赏
  • 举报
回复
这么多估计够呛,友情帮顶
tashiwoweiyi 2011-07-25
  • 打赏
  • 举报
回复

/**
*=--------------------------------------------------------------------=
* class InstantFileRecorder
*=--------------------------------------------------------------------=
*
* 用文件记录及时数据的处理类
*
* 注意:因为对文件数据后期综合分析工作是在第二天凌晨进行,为防程序运行
* 过程中出现异常导致分析不完全,需要设置分析日期时间点的记录文件。
* 这个在分析文件数据的时候,需要通过
*
*/
class InstantFileRecorder
{
private $_dateFileName;
private $_dataFileSurfix;
private $_baseDir;
private $_lastTimestamp;

/**
*=-------------------------------------------------------------------=
*=-------------------------------------------------------------------=
* Public Methods
*=-------------------------------------------------------------------=
*=-------------------------------------------------------------------=
*/

public function __construct($dir = '', $dataFileSurfix = '') {
if (empty($dir)) {
$dir = dirname(__FILE__) . DIRECTORY_SEPARATOR;
}
if (empty($dataFileSurfix)) {
$dataFileSurfix = '_noname.txt';
}
$this->setBaseDir($dir);
$this->setDataFileSurfix($dataFileSurfix);
}

/**
* 将数据记录到文件中
*
* 最终写入数据的文件名为basedir/年月/日_dataFileSurfix
*
* @param string $data - 被写入数据
*/
public function record($data) {
$dir = $this->_baseDir . date('Ym');
if (!is_dir($dir)) {
$this->_mkdirs($dir, 0777);
}
$filename = $dir . DIRECTORY_SEPARATOR . date('d') . $this->_dataFileSurfix;
$data .= "\r\n";
$this->_writeover($filename, $data, 'a+');
}

/**
* 设置文件后缀
*
*
* @param string $str - 文件后缀
*/
public function setDataFileSurfix($str) {
$this->_dataFileSurfix = $str;
}

/**
* 设置分析日期点的记录文件名
*
*
* @param string $str - 文件后缀
*/
public function setDateFileName($str) {
$this->_dateFileName = $str;
}

public function setBaseDir($dir) {
$this->_baseDir = $dir;
}

/**
* useless function, just for adapting instantMemcachedFileRecorder's interface...
*
* @return unknown
*/
public function setQueueLength($len=0) {
$len = intval($len);
if ($len < 1) return false;
}
/**
* 获取文件数据文件所在目录路径
*
* @return unknown
*/
public function getBaseDir() {
return $this->_baseDir;
}

//////////////////////////////////////////////////////////////
// 以下公有方法,用来在读取数据文件 进行后期分析的相关处理
/////////////////////////////////////////////////////////////
/**
* 在整个数据文件读取分析工作完毕之后,需要向日期点记录文件写入分析数据的时间记录
*
* @return unknown
*/
public function recordDate() {
$dir = $this->_baseDir;
if (!is_dir($dir)) {
$this->_mkdirs($dir, 0777);
}
$file_name = $dir . $this->_dateFileName;
$this_timestamp = empty($this->_lastTimestamp) ? time() - 86400 : $this->_lastTimestamp + 86400;
$this->_writeover($file_name, $this_timestamp . '|' . date('Y-m-d', $this_timestamp), 'r+');
}

/**
* 根据初始化的数据文件参数,获取该数据文件所在的完整路径
*
* @return string - 数据文件路径
*/
public function getFullDataFilePath() {
$timestamp = ($this->_lastTimestamp == null) ? $this->getLastAnalyzeTime() : $this->_lastTimestamp;
$timestamp = empty($timestamp) ? time() - 86400 : $timestamp + 86400; //默认分析昨天的数据
$dir = $this->_baseDir . date('Ym', $timestamp);
return $dir . DIRECTORY_SEPARATOR . date('d', $timestamp) . $this->_dataFileSurfix;
}

/**
* 根据设置的日期记录文件 获取 上次分析过的文件数据所在日期时间戳
*
* @return unknown
*/
public function getLastAnalyzeTime() {
$file_name = $this->_baseDir . $this->_dateFileName;
if (!file_exists($file_name)) {
return '';
}
$data = explode('|', file_get_contents($file_name));
$this->_lastTimestamp = isset($data[0]) ? $data[0] : '';
return $this->_lastTimestamp;
}


/**
*=-------------------------------------------------------------------=
*=-------------------------------------------------------------------=
* Private Methods
*=-------------------------------------------------------------------=
*=-------------------------------------------------------------------=
*/

/**
* create multi dirs.
*/
private function _mkdirs($dir,$mode=0664){
if(!is_dir($dir)){
$this->_mkdirs(dirname($dir), $mode);
@mkdir($dir,$mode);
}
return ;
}

/**
* 进行数据文件的写操作。注意本方法是fopen,fclose各一次,所以请慎重使用。
*
* @return unknown
*/
private function _writeover($filename, $data, $method="rb+", $iflock=1, $chmod=1) {
touch($filename);
$handle = fopen($filename, $method);
if (!is_resource($handle)) {
return false;
}
$iflock && flock($handle, LOCK_EX);
fwrite($handle, $data);
if($method == "rb+") ftruncate($handle, strlen($data));
fclose($handle);
$chmod && @chmod($filename, 0777);
return true;
}
}
//设置文件缓存的目录


?>
[/php]

四 使用示例


1. 记录数据处理


$instat_recorder = InstantRecorder::getRecorder('mem', 'cache/instant_data/', '_filename.txt');
$arr_args = array(
'key1' => 'val1',
'key2' => 'val2'
);
$instant_recorder->record(implode(INSTANT_DATA_SEP, $arr_args));


2. 读取数据处理


$instat_recorder = InstantRecorder::getRecorder('file', 'cache/instant_data/', '_filename.txt');
$instat_recorder->setDateFileName('last_analyze_time.inc');
$_analyzing_data_file = $this->_instantRecorder->getFullDataFilePath();
$fp = fopen($this->_analyzingDataFile, 'r+');
if (!$fp) {
return false;
}
while (!feof($fp)) {
// process of analyzing data...
}

[/php]
tashiwoweiyi 2011-07-25
  • 打赏
  • 举报
回复
自己改好了。。。

比较纠结。
皮特张 2011-07-25
  • 打赏
  • 举报
回复
Php 不懂的说。
我嘞个去 2011-07-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 heiboyyang 的回复:]
方法如下:
1.java代码无<?php ?>jsp页面使用<% %>声明变量用!
2.声明变量无 $
3.方法的命名规则
细心改下,lz学习学习
[/Quote]
学习了。。。
HeiBoyYang 2011-07-25
  • 打赏
  • 举报
回复
方法如下:
1.java代码无<?php ?>jsp页面使用<% %>声明变量用!
2.声明变量无 $
3.方法的命名规则
细心改下,lz学习学习
zn85600301 2011-07-25
  • 打赏
  • 举报
回复
估计没人会干这事

51,397

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧