PHP实现java中的DatainputStream类.
在项目中,碰到一个解析流的操作,php里面没有现成的类.
那没办法,只能用二进制模式打开文件,再和java的datainputstream对比着打开文件,一个一个的实现流读取和解析.
废话不多说,代码贴出,其实很简单很简单.
DataInputStream.php
<?php /** * 流解析 * --------------------------------------------- * @Author: Simon 夏向红 sanwkj@163.com * @Date: 2012-02-22 * @package cn.com.nearme.gostore.theme * @name :DataInputStream.php * @version v1.0 * */ class DataInputStream { private $stream = null; /** * 构造函数 * * @param stream of fopen .. $in */ public function __construct( $in ){ $this->stream = $in; } /** * 析构函数 * */ public function __destruct(){ @fclose( $this->stream ); } /** * 读取字节 * * @param int sizeof you want to read $len * @return string */ private function read( $len=1 ){ $len = intval( $len ); if( $len>0 ) { if( !feof( $this->stream ) ) { $read = @fread($this->stream,$len); if( strlen($read)!=$len ){ throw new Exception('流文件已经到末尾,读取错误',222); } else { return $read; } } else { throw new Exception('流文件已经到末尾,读取错误',222); } } else { return ''; } } /** * 读取一个字节,并转换成int * * @return int */ public function readByte(){ return hexdec(bin2hex($this->read(1))); } /** * 短整形 * * @return int */ public function readShort(){ return hexdec(bin2hex($this->read(2))); } /** * 整形数据 * * @return int */ public function readInt(){ return hexdec(bin2hex($this->read(4))); } /** * 读取一个字符串 * * @return string */ public function readUTF(){ $len = $this->readShort(); return $this->read($len); } /** * 读取指定长度的数据,并作为字符串返回,图片等二进制文件,可能需要这样读取. * * @param int $len * @return unknown */ public function readFully( $len ) { return $this->read( $len ); } /** * readUTF的别名 * * @return unknown */ public function readString(){ return $this->readUTF(); } /** * 长整形. * * @return unknown */ public function readLong(){ return hexdec(bin2hex($this->read(8))); } } ?>
登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章
如何修復KB5055523無法在Windows 11中安裝?
4 週前
By DDD
如何修復KB5055518無法在Windows 10中安裝?
4 週前
By DDD
<🎜>:種植花園 - 完整的突變指南
3 週前
By DDD
<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前
By 尊渡假赌尊渡假赌尊渡假赌
如何修復KB5055612無法在Windows 10中安裝?
3 週前
By DDD

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)