PushbackInputStream 语法器的运用
/**
* 语法解析器:PushbackInputStream
* 举例1:解析语句"for(int i=0;i<10;i++)"中的for关键字,扫描到"for"是不是就可以说是个关键字了呢?不行,说不定后面是“for1”,那就是个变量而不是关键字了,知道看到“(”才恍然大悟,哦,我可以安全地说“看到for关键字”了,但“(”还得归还给输入流,因为需要后面继续扫描。
* 举例2:解析HTML文档的时候,我需要根据它的“meta”标签的“charset”属性来决定使用哪种字符集进行解析,但HTML可不是“charset”而是“<html>”开头的哦!所以需要通过PushbackInputStream缓冲前面一段内容,等取到字符集名称后在把读到的流全部归还,再用指定的字符集进行解析。
* 举例3:解析文件内容中的英文字母还是中文汉字。
*/
import java.io.*;
public class PushbackInputStreamDemo
{
public static void main(String[] args)
{
System.out.println("\r\n示例1:PushbackInputStream.read()单字节解析器");
String str = "if (a == 4) a = 0 ;";
byte[] bArray = str.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(bArray);
try(PushbackInputStream pin = new PushbackInputStream(in)){
int c;
while( (c=pin.read()) !=-1 ){
switch(c){
case '=':
if( (c=pin.read()) == '=' ){
System.out.print(".equals.");
}else{
System.out.print("->");
pin.unread(c);
}
break;
default:
System.out.print((char)c);
break;
}
}
System.out.println();
}catch(Exception e){}
System.out.println("\r\n示例2:PushbackInputStream.read(array[2])双字节解析器");
String str2 = "if (a == 4) a = 0 ;";
byte[] bArray2 = str2.getBytes();
ByteArrayInputStream in2 = new ByteArrayInputStream(bArray2);
try(PushbackInputStream pin2 = new PushbackInputStream(in2)){
int count;
byte[] array = new byte[2];
while( pin2.available()>1 && (count=pin2.read(array)) !=-1 ){
if( (new String(array)).equals("==") ){
System.out.print(".equals.");
}
else if( (new String(array)).equals("= ") ){
System.out.print("->");
pin2.unread(array[1]);
}
else{
String ss;
System.out.print(ss = new String(array));
if(ss.contains("="))
pin2.unread(array[1]);
}
}
if(pin2.available()>0){
System.out.println((char)pin2.read());
}
System.out.println();
}catch(Exception e){}
System.out.println("\r\n示例3:PushbackReader.read()单字符解析器");
String str3 = "if (a == 4) a = 0 ;";
char[] cArray3 = new char[str3.length()];
str3.getChars(0,str3.length(),cArray3,0);
CharArrayReader in3 = new CharArrayReader(cArray3);
try(PushbackReader pin3 = new PushbackReader(in3)){
int c;
while( (c=pin3.read()) !=-1 ){
switch(c){
case '=':
if( (c=pin3.read()) == '=' ){
System.out.print(".equals.");
}else{
System.out.print("->");
pin3.unread(c);
}
break;
default:
System.out.print((char)c);
break;
}
}
System.out.println();
}catch(Exception e){}
System.out.println("\r\n示例4:PushbackInputStream.read(array[2])双字节解析器:解析英文和中文");
String str0 = "a我b是c学d生;";
byte[] bArray0 = str0.getBytes();
ByteArrayInputStream in0 = new ByteArrayInputStream(bArray0);
try( PushbackInputStream pin0 = new PushbackInputStream(in0) )
{
byte[] array0 = new byte[2];
int tmp = 0;
int count = 0;
while( pin0.available()>1 && (count = pin0.read(array0)) != -1 )
{
//两个字节转换为整数
tmp = (short)((array0[0] << 8) | (array0[1] & 0xff));
tmp = tmp & 0xFFFF;
//判断是否为BIG5,如果是则显示BIG5中文字
if(tmp >= 0xA440 && tmp < 0xFFFF)
{
System.out.println("BIG5:" + new String(array0));
}
else
{
//将第二个字节推回流
pin0.unread(array0[1]);
//显示ASCII范围的字符
System.out.println("ASCII: " + (char)array0[0]);
}
}
if( pin0.available()>0 )
System.out.println((char)pin0.read());
}
catch(Exception e){ e.printStackTrace(); }
}
}