在項(xiàng)目中,碰到一個(gè)解析流的操作,php里面沒有現(xiàn)成的類.?
那沒辦法,只能用二進(jìn)制模式打開文件,再和java的datainputstream對(duì)比著打開文件,一個(gè)一個(gè)的實(shí)現(xiàn)流讀取和解析.?
廢話不多說,代碼貼出,其實(shí)很簡(jiǎn)單很簡(jiǎn)單.?
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; /** * 構(gòu)造函數(shù) * * @param stream of fopen .. $in */ public function __construct( $in ){ $this->stream = $in; } /** * 析構(gòu)函數(shù) * */ public function __destruct(){ @fclose( $this->stream ); } /** * 讀取字節(jié) * * @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('流文件已經(jīng)到末尾,讀取錯(cuò)誤',222); } else { return $read; } } else { throw new Exception('流文件已經(jīng)到末尾,讀取錯(cuò)誤',222); } } else { return ''; } } /** * 讀取一個(gè)字節(jié),并轉(zhuǎn)換成int * * @return int */ public function readByte(){ return hexdec(bin2hex($this->read(1))); } /** * 短整形 * * @return int */ public function readShort(){ return hexdec(bin2hex($this->read(2))); } /** * 整形數(shù)據(jù) * * @return int */ public function readInt(){ return hexdec(bin2hex($this->read(4))); } /** * 讀取一個(gè)字符串 * * @return string */ public function readUTF(){ $len = $this->readShort(); return $this->read($len); } /** * 讀取指定長(zhǎng)度的數(shù)據(jù),并作為字符串返回,圖片等二進(jìn)制文件,可能需要這樣讀取. * * @param int $len * @return unknown */ public function readFully( $len ) { return $this->read( $len ); } /** * readUTF的別名 * * @return unknown */ public function readString(){ return $this->readUTF(); } /** * 長(zhǎng)整形. * * @return unknown */ public function readLong(){ return hexdec(bin2hex($this->read(8))); } } ?>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article
Guide: Stellar Blade Save File Location/Save File Lost/Not Saving
4 weeks ago
By DDD
Oguri Cap Build Guide | A Pretty Derby Musume
2 weeks ago
By Jack chen
Agnes Tachyon Build Guide | A Pretty Derby Musume
2 weeks ago
By Jack chen
Dune: Awakening - Advanced Planetologist Quest Walkthrough
4 weeks ago
By Jack chen
Date Everything: Dirk And Harper Relationship Guide
4 weeks ago
By Jack chen

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)
