⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sipmessageparser.java

📁 jsr-180 (SIP) 实现源码。可以在真实手机上使用
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.micromethod.sipstack.ri;

import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Vector;

import com.micromethod.sipstack.i.SipAddress;
import com.micromethod.sipstack.i.SipException;

/**
 * SipMessageParser
 */
public class SipMessageParser {

  public static final int _fldbyte = 0;

  public static final int e = 1;

  public static final int d = 2;

  public static final int c = 3;

  public static final int _fldchar = 4;

  public static final int _fldelse = 5;

  public static final int _fldgoto = 6;

  public static final int h = 7;

  public static final int g = 8;

  public static final int _fldnew = 9;

  public static final int b = 10;

  public static final int _fldfor = 11;

  public static final int _fldvoid = 12;

  public static final int a = 13;

  public static final int _fldif = 13;

  public static final int _fldtry = 10;

  public static final int _fldnull = 92;

  /**
   * m_buf
   */
  private byte m_buf[] = null;

  /**
   * m_substitute
   */
  private byte m_substitute = 0;

  /**
   * m_headerName
   */
  private String m_headerName = null;

  /**
   * m_correct
   */
  private boolean m_correct = false;

  /**
   * m_errorMessage
   */
  private String m_errorMessage = null;

  /**
   * SipMessageParser Constructor
   */
  public SipMessageParser() {
    m_buf = new byte[120];
    m_substitute = 32;
    m_headerName = null;
    m_correct = true;
    m_errorMessage = null;
  }

  /**
   * setSubstitute, instead of ' ' and '\t'
   * 
   * @param newSubstitute
   *          newSubstitute substitute char
   * @return old substitute
   */
  public byte setSubstitute(byte newSubstitute) {
    byte byte1 = m_substitute;
    m_substitute = newSubstitute;
    return byte1;
  }

  /**
   * getSipMessage
   * 
   * @param sipMessage
   * @return assembledSipMessage
   * @throws IOException
   * @throws SipException
   */
  public SipMessage getSipMessage(InputStream sipMessage) throws IOException,
      SipException {
    Vector vector = new Vector();
    Vector vector1 = new Vector();
    parseFirstLine(sipMessage, vector);
    if (vector.size() > 3) {
      m_correct = false;
      m_errorMessage = "1 Line is not correct";
    }
    parseHeaders(sipMessage, vector1);
    PairStructure t1 = removeHeader(vector1, "Content-Type");
    byte abyte0[] = getContent(sipMessage, vector1, t1);
    return assembledSipMessage(vector, vector1, abyte0, t1);
  }

  /**
   * parseFirstLine
   * 
   * @param sipMessage
   * @param firstLine
   * @throws IOException
   * @throws SipException
   */
  private void parseFirstLine(InputStream sipMessage, Vector firstLine)
      throws IOException, SipException {
    boolean flag = false;
    int k = 0;
    int l = 0;
    char ac[] = new char[2];
    boolean flag1 = false;
    do {
      int i;
      if ((i = sipMessage.read()) == -1)
        break;
      if (i == 37) {
        ac[0] = (char) sipMessage.read();
        ac[1] = (char) sipMessage.read();
        try {
          i = Integer.parseInt(new String(ac), 16);
        }
        catch (Exception exception) {
          m_correct = false;
          m_errorMessage = "Problem with % in 1Line";
          if ((i = sipMessage.read()) != -1) {
            break;
          }
        }
      }
      switch (l) {
        case 0: // '\0'
          switch (i) {
            default:
              k = addByteData(i, k);
              l = 1;
              break;

            case 9: // '\t'
            case 10: // '\n'
            case 13: // '\r'
            case 32: // ' '
              break;
          }
          break;

        case 1: // '\001'
          switch (i) {
            case 32: // ' '
              l = 2;
              k = addFirstLinePart(firstLine, k);
              break;

            default:
              k = addByteData(i, k);
              break;
          }
          break;

        case 2: // '\002'
          switch (i) {
            case 32: // ' '
              k = addFirstLinePart(firstLine, k);
              l = 3;
              break;

            case 13: // '\r'
              i = sipMessage.read();
              if (i == 10) {
                flag1 = true;
              }
              else {
                m_correct = false;
                flag1 = true;
                m_errorMessage = "CR without LF in 1Line";
              }
              break;

            case 10: // '\n'
              flag1 = true;
              break;

            default:
              k = addByteData(i, k);
              break;
          }
          break;

        case 3: // '\003'
          switch (i) {
            case 13: // '\r'
              i = sipMessage.read();
              if (i == 10) {
                flag1 = true;
              }
              else {
                m_correct = false;
                flag1 = true;
                m_errorMessage = "CR without LF in 1Line";
              }
              break;

            case 10: // '\n'
              flag1 = true;
              break;

            default:
              k = addByteData(i, k);
              break;
          }
          break;
      }
    }
    while (!flag1);
    if (k != 0) {
      k = addFirstLinePart(firstLine, k);
    }
  }

  /**
   * parseHeaders
   * 
   * @param sipMessage
   * @param headers
   * @throws IOException
   * @throws SipException
   */
  private void parseHeaders(InputStream sipMessage, Vector headers)
      throws IOException, SipException {
    boolean flag = false;
    int k = 0;
    boolean flag1 = false;
    boolean flag2 = false;
    char ac[] = new char[2];
    byte byte0 = 4;
    byte byte1 = 4;
    do {
      int i;
      if ((i = sipMessage.read()) == -1)
        break;
      if (i == 37) {
        ac[0] = (char) sipMessage.read();
        ac[1] = (char) sipMessage.read();
        try {
          i = Integer.parseInt(new String(ac), 16);
        }
        catch (Exception exception) {
          m_correct = false;
          m_errorMessage = "Problem with % in Header";
          if ((i = sipMessage.read()) != -1)
            break;
        }
      }
      switch (byte0) {
        case 4: // '\004'
          switch (i) {
            case 13: // '\r'
              byte0 = 8;
              byte1 = 4;
              break;

            case 10: // '\n'
              byte0 = 9;
              byte1 = 4;
              break;

            case 58: // ':'
              if (k > 0) {
                k = parseHeaderNamePart(k);
                byte0 = 6;
                byte1 = 7;
              }
              else {
                throw new SipException("Header without value", (byte) 0);
              }
              break;

            default:
              k = addByteData(i, k);
              break;

            case 9: // '\t'
            case 32: // ' '
              break;
          }
          break;

        case 6: // '\006'
          switch (i) {
            case 13: // '\r'
              byte0 = 8;
              break;

            case 10: // '\n'
              byte0 = 9;
              break;

            case 9: // '\t'
            case 32: // ' '
              byte0 = 6;
              break;

            case 34: // '"'
              k = addByteData(i, k);
              byte0 = 10;
              break;

            case 59: // ';'
              m_correct = false;
              k = addByteData(i, k);
              m_errorMessage = "; in header start";
              break;

            default:
              k = addByteData(i, k);
              byte0 = 7;
              break;
          }
          break;

        case 7: // '\007'
          switch (i) {
            case 13: // '\r'
              byte0 = 8;
              break;

            case 10: // '\n'
              byte0 = 9;
              break;

            case 34: // '"'
              k = addByteData(i, k);
              byte0 = 10;
              break;

            case 9: // '\t'
            case 32: // ' '
              k = addByteData(((int) (m_substitute)), k);
              break;

            case 44: // ','
              if (m_headerName != null && m_headerName.equals("Expires")) {
                k = addByteData(i, k);
              }
              else {
                k = addHeader(headers, k);
                byte0 = 6;
              }
              break;

            default:
              k = addByteData(i, k);
              byte0 = 7;
              break;
          }
          break;

        case 8: // '\b'
          switch (i) {
            case 13: // '\r'
              k = addHeader(headers, k);
              byte0 = 4;
              break;

            case 10: // '\n'
              byte0 = 9;
              break;

            case 9: // '\t'
            case 32: // ' '
              k = addByteData(((int) (m_substitute)), k);
              byte0 = byte1;
              break;

            default:
              k = addHeader(headers, k);
              byte0 = 4;
              k = addByteData(i, k);
              if (byte1 == 10)
                m_correct = false;
              m_errorMessage = "End of line with hyphen but not continue";
              break;
          }
          break;

        case 9: // '\t'
          switch (i) {
            case 10: // '\n'
              flag2 = true;
              break;

            case 9: // '\t'
            case 32: // ' '
              k = addByteData(((int) (m_substitute)), k);
              byte0 = byte1;
              break;

            default:
              k = addHeader(headers, k);
              k = addByteData(i, k);
              byte0 = 4;
              if (byte1 == 10)
                m_correct = false;
              m_errorMessage = "End of line in Hyphen but line not continue";
              break;

            case 13: // '\r'
              break;
          }
          break;

        case 10: // '\n'
          switch (i) {
            case 34: // '"'
              k = addByteData(i, k);
              byte0 = byte1;
              break;

            case 92: // '\\'
              i = sipMessage.read();
              k = addByteData(i, k);
              break;

            case 13: // '\r'
              byte1 = 10;
              byte0 = 8;
              break;

            case 10: // '\n'

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -