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.sdp;
025:
026: import java.io.*;
027: import java.util.*;
028:
029: public class Parser {
030: private static Vector buffer;
031:
032: public void init() {
033: buffer = new Vector();
034: }
035:
036: public String getTag(ByteArrayInputStream bin) {
037: ByteArrayOutputStream bout = new ByteArrayOutputStream();
038:
039: skipWhitespace(bin);
040:
041: if (bin.available() > 0) {
042: int ch = readChar(bin);
043:
044: while (ch != '=' && ch != '\n' && ch != '\r' && ch != -1) {
045: bout.write(ch);
046:
047: ch = readChar(bin);
048: }
049:
050: bout.write(ch);
051: }
052:
053: String tag = new String(bout.toByteArray());
054:
055: return tag;
056: }
057:
058: public void ungetTag(String tokenStr) {
059: byte token[] = tokenStr.getBytes();
060:
061: for (int i = 0; i < token.length; i++) {
062: buffer.insertElementAt(new Integer(token[token.length - i
063: - 1]), 0);
064: }
065: }
066:
067: public String getLine(ByteArrayInputStream bin) {
068: ByteArrayOutputStream bout = new ByteArrayOutputStream();
069:
070: if (bin.available() > 0) {
071: int ch = readChar(bin);
072:
073: while (ch != '\n' && ch != '\r' && ch != -1) {
074: bout.write(ch);
075:
076: ch = readChar(bin);
077: }
078: }
079:
080: String line = new String(bout.toByteArray());
081:
082: return line;
083: }
084:
085: private void skipWhitespace(ByteArrayInputStream bin) {
086: int ch = readChar(bin);
087:
088: while (ch == ' ' || ch == '\n' || ch == '\r') {
089: ch = readChar(bin);
090: }
091:
092: buffer.insertElementAt(new Integer(ch), 0);
093: }
094:
095: public int readChar(ByteArrayInputStream bin) {
096: int ch;
097:
098: if (buffer.size() > 0) {
099: ch = ((Integer) buffer.elementAt(0)).intValue();
100:
101: buffer.removeElementAt(0);
102: } else {
103: ch = bin.read();
104: }
105:
106: return ch;
107: }
108:
109: private boolean print_debug = false;
110:
111: public void debug(String str) {
112: if (print_debug) {
113: System.out.println(str);
114: }
115: }
116: }
|