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

📄 xbfparser.java

📁 java开源的企业总线.xmlBlaster
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
      String qos = null;      for (int ii=0; ii<Integer.MAX_VALUE; ii++) {         qos = toString(buf);         if (buf.offset >= buf.buf.length) {            if (qos.length() > 0) {               MsgUnitRaw msgUnit = new MsgUnitRaw(null, (byte[])null, qos);               msgInfo.addMessage(msgUnit);            }            break;         }         String key = toString(buf);         if (buf.offset >= buf.buf.length) {            MsgUnitRaw msgUnit = new MsgUnitRaw(key, (byte[])null, qos);            msgInfo.addMessage(msgUnit);            break;         }         if (log.isLoggable(Level.FINE)) log.fine("Getting messageUnit #" + ii);         MsgUnitRaw msgUnit = new MsgUnitRaw(key, toByte(buf), qos);         msgInfo.addMessage(msgUnit);         if (buf.offset >= buf.buf.length) break;      }      if (msgInfo.isChecksum())         checkSumResult = toLong0(buf, -1);      if (buf.offset != buf.buf.length) {         String str = "Format mismatch, read index=" + buf.offset + " expected message length=" + buf.buf.length + " we need to disconnect the client, can't recover.";         throw new IOException(ME + ": " + str);      }      if (log.isLoggable(Level.FINE)) log.fine("Leaving parse(), message successfully parsed");      return new MsgInfo[] { msgInfo };   }   /**    * Returns a raw data string.    * <pre>    *  msgLen[10] flag[6] requestId methodName sessionId  lenUnzipped  userData  checkSum[10]    *  +---------+-------+------ -*----------*-----------*-----------*-----------+----------+    *    *    *  The 'userData' consists of 0-n of these:    *    *  qos      key    len   content    *  +-----*---------*-----*----------+    *    *  An example is ('*' marks a null byte):    *    *  "        83**I**17711*publish*oxf6hZs**<qos></qos>*<key oid='hello'/>*11*Hello world"    *    * </pre>    */   public final byte[] createRawMsg(MsgInfo msgInfo) throws XmlBlasterException {      try {         long len = msgInfo.getUserDataLen() + 500;         ByteArray out = new ByteArray((int)len);            /*         int lenProxyHeader = 0;         if (proxyHost != null) {             telnet proxy 3128             GET http://192.121.221.46:8080 HTTP/1.0             POST /path/script.cgi HTTP/1.0             From: frog@jmarshall.com             User-Agent: HTTPxmlBlaster/1.0             Content-Type: application/x-www-form-urlencoded             Content-Length: 32             home=Cosby&favorite+flavor=flies            final byte[] CRLF = {13, 10};            final String CRLFstr = new String(CRLF);            StringBuffer buf = new StringBuffer(256);            buf.append("POST http://").append(hostname).append(":").append(port).append(" HTTP/1.0").append(CRLFstr);         }            */         out.write(EMPTY10, 0, EMPTY10.length); // Reserve 10 bytes at the beginning ...         // Write the 6 byte fields ...         out.write((msgInfo.isChecksum())?CHECKSUM_ADLER_BYTE:NULL_BYTE);    // 'A'         out.write((msgInfo.isCompressed())?COMPRESSED_GZIP_BYTE:NULL_BYTE); // 'Z'         out.write(msgInfo.getType()); // 'I' or 'R' or 'E'         out.write(NULL_BYTE);       // byte4         out.write(NULL_BYTE);       // byte5         out.write(VERSION_1_BYTE);  // '1'         out.write(msgInfo.createRequestId(null).getBytes());         out.write(NULL_BYTE);         out.write(msgInfo.getMethodName().getMethodNameBytes());         out.write(NULL_BYTE);         out.write(msgInfo.getSecretSessionId().getBytes());         out.write(NULL_BYTE);         if (lenUnzipped > 0)            out.write(new String(""+lenUnzipped).getBytes());         out.write(NULL_BYTE);         for (int ii=0; ii<msgInfo.getMessages().size(); ii++) {            MsgUnitRaw unit = (MsgUnitRaw)msgInfo.getMessages().elementAt(ii);            out.write(unit.getQos().getBytes());            out.write(NULL_BYTE);            out.write(unit.getKey().getBytes());            out.write(NULL_BYTE);            byte[] tmp = unit.getContent();            out.write((""+tmp.length).getBytes());            out.write(NULL_BYTE);            out.write(tmp);         }         if (msgInfo.isChecksum() == true) {            int pos = out.size();            out.write(EMPTY10, 0, EMPTY10.length);            byte[] checkSumResultB = new String(""+checkSumResult).getBytes();            out.insert(pos+EMPTY10.length-checkSumResultB.length, checkSumResultB);         }         // Finally we know the overall length, write it to the header:         byte[] msgLengthB = new String(""+out.size()).getBytes();         out.insert(EMPTY10.length - msgLengthB.length, msgLengthB);         return out.toByteArray();      }      catch(IOException e) {         String text = "Creation of message failed.";         log.warning(text + " " + e.toString());         throw new XmlBlasterException(glob, ErrorCode.INTERNAL_UNKNOWN, ME, text, e);      }   }   /**    * Reads the binary content of a message. First we parse the long value which    * holds the content length, than we retrieve the binary content.    */   public final byte[] toByte(Buf buf) throws IOException {      int len = toInt0(buf, 0);      byte[] b = new byte[len];      if (len == 0L)         return b;      System.arraycopy(buf.buf, buf.offset, b, 0, len);      buf.offset += len;      return b;   }   /**    * Converts bytes from byte[] until \0 to a long    */   public final long toLong0(Buf buf, long defaultVal) throws IOException {      String tmp = toString(buf).trim();      if (tmp == null || tmp.length() < 1)         return defaultVal;      try {         return Long.parseLong(tmp);      }      catch (NumberFormatException e) {         e.printStackTrace();         log.severe("toLong0(" + niceAndShort(tmp) + ") " + buf.toLiteral());         throw new IOException(ME + ": Format is corrupted '" + toString() + "', expected long integral value");      }   }   /**    * Converts bytes from byte[] until \0 to an int    */   private final int toInt0(Buf buf, int defaultVal) throws IOException {      String tmp = toString(buf).trim();      if (tmp == null || tmp.length() < 1)         return defaultVal;      try {         return Integer.parseInt(tmp.trim());      }      catch (NumberFormatException e) {         e.printStackTrace();         log.severe("toInt0(" + niceAndShort(tmp) + ") " + buf.toLiteral());         throw new IOException(ME + ": Format is corrupted '" + toString() + "', expected integral value");      }   }   private String niceAndShort(String tmp)   {      if (tmp == null)         return "null";      if (tmp.length() > 50)         return tmp.substring(0,50) + " ...";      return tmp;   }   /**    * Extracts string until next null byte '\0'    */   private final String toString(Buf buf) throws IOException  {      int startOffset = buf.offset;      for (; buf.offset<buf.buf.length; buf.offset++) {         if (buf.buf[buf.offset] == 0) {            if (startOffset == buf.offset) {               buf.offset++;  // overread the 0               return EMPTY_STRING;            }            buf.offset++;  // overread the 0            return new String(buf.buf, startOffset, buf.offset-startOffset-1);         }      }      //if (buf.offset == buf.buf.length)      //   return EMPTY_STRING;      return new String(buf.buf, startOffset, buf.offset-startOffset);   }   /**    * Get the raw messages as a string, for tests and for dumping only    * @return The stringified message, null bytes are replaced by '*'    */   public final String toLiteral(MsgInfo msgInfo) throws XmlBlasterException {      return createLiteral(createRawMsg(msgInfo));   }   public final String toLiteral(byte[] arr) {      return createLiteral(arr);   }      /**    * Get the raw messages as a string, for tests and for dumping only    * @return The stringified message, null bytes are replaced by '*'    */   public static final String createLiteral(byte[] arr) {      StringBuffer buffer = new StringBuffer(arr.length+10);      byte[] dummy = new byte[1];      for (int ii=0; ii<arr.length; ii++) {         if (arr[ii] == 0)            buffer.append("*");         else {            dummy[0] = arr[ii];            buffer.append(new String(dummy));         }      }      return buffer.toString();   }   private class Buf {      byte[] buf; // Holding one message      int offset; // Current position of reading      public String toString() {         if (buf == null) return "null";         byte[] tmp = new byte[buf.length];         for (int ii=0; ii<buf.length; ii++) {            if (buf[ii] == 0)               tmp [ii] = (byte)'*';            else               tmp[ii] = buf[ii];         }         return new String(tmp);      }      /**       * Get the current section of buf as a string -20 to + 100 bytes       * @return The stringified message, null bytes are replaced by '*'       */      public String toLiteral() {         StringBuffer buffer = new StringBuffer(200);         int start = 0;         if (offset > 20)            start = offset-20;         buffer.append("Dumping from offset=" + start + ", problemOffset=" + offset + " msgLen=" + buf.length + ": '");         byte[] dummy = new byte[1];         int ii=start;         for (; ii<offset+100 && ii<buf.length; ii++) {            if (buf[ii] == 0)               buffer.append("*");            else {               dummy[0] = buf[ii];               buffer.append(new String(dummy));            }         }         buffer.append("'");         if (ii < buf.length)            buffer.append(" ...");         return buffer.toString();      }   }   /**    * java org.xmlBlaster.util.xbformat.XbfParser    * See: java org.xmlBlaster.util.xbformat.MsgInfo    */   /*   public static void main( String[] args ) {      try {         Global glob = new Global(args);

⌨️ 快捷键说明

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