📄 wsptokenizer.java
字号:
/*
* JVending - J2ME MMS Client
* Copyright (C) 2004 Shane Isbell
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.jvending.messaging.impl.mms;
import java.io.InputStream;
import java.io.IOException;
import java.util.NoSuchElementException;
import org.jvending.messaging.impl.io.PeekInputStream;
import org.jvending.messaging.impl.util.MmsDecodingUtility;
/**
* @author Shane Isbell
* @version 1.0.0a
* @created 04/03/27
*/
final class WspTokenizer implements Tokenizer {
private PeekInputStream dis;
private WspTokenType wspTokenType;
private Object tokenObject;
private DataOctetTokenizer dataTokenizer;
private boolean dataOctetFlag = false;
private int length;
private int tokenState;
private SimpleQueue tokenStateQueue;
WspTokenizer(InputStream dis) {
setInputStream(dis);
}
WspTokenizer(PeekInputStream dis) {
this.dis = dis;
}
void setDataOctetTokenizer(DataOctetTokenizer dataTokenizer) {
this.dataTokenizer = dataTokenizer;
}
public void setInputStream(InputStream is) {
dis = new PeekInputStream(is);
}
public void setInputStream(PeekInputStream is) {
dis = is;
}
public Object nextElement() throws NoSuchElementException {
WspToken t = null;
int i = -10;
try {
if(dataOctetFlag && tokenStateQueue != null && !tokenStateQueue.isEmpty() ) {
t = getTokenFromDataOctet();
return (Object) t;
} else {
dataOctetFlag = false;
}
i = dis.readByte();
//System.out.println("BYTE:" + i);
if(i >= 0 && i <= 31) {
tokenObject = new Integer(i);
wspTokenType = WspToken.SHORT_LENGTH;
length = i;
dataOctetFlag = true;
tokenStateQueue = dataTokenizer.getTokenStateQueue(tokenState, dis.peekByte(1));
} else if(i == 31) {
tokenObject = MmsDecodingUtility.readUintvarInteger(dis);
wspTokenType = WspToken.UINTVAR_INTEGER;
} else if(i == 34) {
tokenObject = MmsDecodingUtility.readString(dis);
wspTokenType = WspToken.QUOTED_STRING;
} else if(i == 127) {
tokenObject = MmsDecodingUtility.readString(dis);
wspTokenType = WspToken.TEXT_STRING;
} else if(i >= 32 && i <= 127) {
tokenObject = MmsDecodingUtility.readExtensionMedia(dis, i);
wspTokenType = WspToken.EXTENSION_MEDIA;
} else if(i >= 128 && i <= 255) {// anything above 127 consists of only one byte
tokenObject = new Integer((i & 0x7f));
wspTokenType = WspToken.SHORT_INTEGER;
tokenState = i;
} else if(i == -1) {
tokenObject = null;
wspTokenType = WspToken.EOF;
} else {
throw new NoSuchElementException("Illegal Octet: " + i);
}
} catch(IOException e) {
throw new SecurityException("IOException occurred:" + i);
}
t = new WspToken(wspTokenType, (Object) tokenObject);
return ((Object) t);
}
public boolean hasMoreElements() {
return wspTokenType != WspToken.EOF;
}
private WspToken getTokenFromDataOctet() throws IOException {
wspTokenType = (WspTokenType) tokenStateQueue.dequeue();
if(wspTokenType == WspToken.MULTI_OCTET_INTEGER) {
tokenObject = MmsDecodingUtility.readMultiOctetInteger(dis, length);
} else if(wspTokenType == WspToken.SHORT_LENGTH) {
length = dis.readUnsignedByte();
tokenObject = new Integer(length);
} else if(wspTokenType == WspToken.SHORT_INTEGER) {
tokenObject = new Integer((dis.readUnsignedByte() & 0x7f));
} else if(wspTokenType == WspToken.TEXT_STRING) {
tokenObject = MmsDecodingUtility.readString(dis);
} else if(wspTokenType == WspToken.DATA_OCTETS) {
tokenObject = MmsDecodingUtility.readDataOctets(dis, length);
}
return (new WspToken(wspTokenType, (Object) tokenObject));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -