001: /*
002: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License version
007: * 2 only, as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful, but
010: * WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * General Public License version 2 for more details (a copy is
013: * included at /legal/license.txt).
014: *
015: * You should have received a copy of the GNU General Public License
016: * version 2 along with this work; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
018: * 02110-1301 USA
019: *
020: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
021: * Clara, CA 95054 or visit www.sun.com if you need additional
022: * information or have any questions.
023: */
024: package com.sun.mmedia.rtsp.protocol;
025:
026: import java.io.*;
027: import java.util.*;
028:
029: public class Parser {
030: private Vector buffer;
031:
032: public Parser() {
033: init();
034: }
035:
036: public int readChar(ByteArrayInputStream bin) {
037: int ch;
038:
039: if (buffer.size() > 0) {
040: ch = ((Integer) buffer.elementAt(0)).intValue();
041:
042: buffer.removeElementAt(0);
043: } else {
044: ch = bin.read();
045: }
046:
047: return ch;
048: }
049:
050: public String getToken(ByteArrayInputStream bin) {
051: ByteArrayOutputStream bout = new ByteArrayOutputStream();
052:
053: skipWhitespace(bin);
054:
055: if (bin.available() > 0) {
056: int ch = readChar(bin);
057:
058: while (ch != ' ' && ch != '\n' && ch != '\r' && ch != -1) {
059: bout.write(ch);
060:
061: ch = readChar(bin);
062: }
063:
064: ungetChar(ch);
065: }
066:
067: String token = new String(bout.toByteArray());
068:
069: return token;
070: }
071:
072: public void ungetChar(int ch) {
073: buffer.insertElementAt(new Integer(ch), 0);
074: }
075:
076: public String getLine(ByteArrayInputStream bin) {
077: ByteArrayOutputStream bout = new ByteArrayOutputStream();
078:
079: int ch = readChar(bin);
080:
081: while (ch != '\n' && ch != '\r' && ch != -1) {
082: bout.write(ch);
083:
084: ch = readChar(bin);
085: }
086:
087: ch = readChar(bin);
088:
089: if (ch != '\n') {
090: ungetChar(ch);
091: }
092:
093: String line = new String(bout.toByteArray());
094:
095: return line;
096: }
097:
098: public String getStringToken(ByteArrayInputStream bin) {
099: ByteArrayOutputStream bout = new ByteArrayOutputStream();
100:
101: skipWhitespace(bin);
102:
103: int ch = readChar(bin);
104:
105: while (ch != '\n' && ch != '\r' && ch != -1) {
106: bout.write(ch);
107:
108: ch = readChar(bin);
109: }
110:
111: String token = new String(bout.toByteArray());
112:
113: return token;
114: }
115:
116: public byte[] getContent(ByteArrayInputStream bin) {
117: ByteArrayOutputStream bout = new ByteArrayOutputStream();
118:
119: skipWhitespace(bin);
120:
121: int ch = readChar(bin);
122:
123: while (ch != -1) {
124: bout.write(ch);
125:
126: ch = readChar(bin);
127: }
128:
129: return bout.toByteArray();
130: }
131:
132: private void skipWhitespace(ByteArrayInputStream bin) {
133: int ch = readChar(bin);
134:
135: while (ch == ' ' || ch == '\n' || ch == '\r') {
136: ch = readChar(bin);
137: }
138:
139: ungetChar(ch);
140: }
141:
142: private void init() {
143: buffer = new Vector();
144: }
145: }
|