Home » synapse-1.2-src » org.apache.synapse.format.hessian » [javadoc | source]

    1   /*
    2    *  Licensed to the Apache Software Foundation (ASF) under one
    3    *  or more contributor license agreements.  See the NOTICE file
    4    *  distributed with this work for additional information
    5    *  regarding copyright ownership.  The ASF licenses this file
    6    *  to you under the Apache License, Version 2.0 (the
    7    *  "License"); you may not use this file except in compliance
    8    *  with the License.  You may obtain a copy of the License at
    9    *
   10    *   http://www.apache.org/licenses/LICENSE-2.0
   11    *
   12    *  Unless required by applicable law or agreed to in writing,
   13    *  software distributed under the License is distributed on an
   14    *   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   15    *  KIND, either express or implied.  See the License for the
   16    *  specific language governing permissions and limitations
   17    *  under the License.
   18    */
   19   
   20   package org.apache.synapse.format.hessian;
   21   
   22   import java.io.IOException;
   23   import java.io.OutputStream;
   24   
   25   /**
   26    * Util class for writing the Hessian Fault to the output stream
   27    */
   28   public class HessianUtils {
   29   
   30       public static void printString(String v, int offset, int length, OutputStream os) throws IOException {
   31           for (int i = 0; i < length; i++) {
   32               char ch = v.charAt(i + offset);
   33   
   34               if (ch < 0x80)
   35                   os.write(ch);
   36               else if (ch < 0x800) {
   37                   os.write(0xc0 + ((ch >> 6) & 0x1f));
   38                   os.write(0x80 + (ch & 0x3f));
   39               }
   40               else {
   41                   os.write(0xe0 + ((ch >> 12) & 0xf));
   42                   os.write(0x80 + ((ch >> 6) & 0x3f));
   43                   os.write(0x80 + (ch & 0x3f));
   44               }
   45           }
   46       }
   47   
   48       public static void writeString(String value, OutputStream os) throws IOException {
   49           if (value == null) {
   50               os.write('N');
   51           }
   52           else {
   53               int length = value.length();
   54               int offset = 0;
   55   
   56               while (length > 0x8000) {
   57                   int sublen = 0x8000;
   58   
   59                   os.write('s');
   60                   os.write(sublen >> 8);
   61                   os.write(sublen);
   62   
   63                   printString(value, offset, sublen, os);
   64   
   65                   length -= sublen;
   66                   offset += sublen;
   67               }
   68   
   69               os.write('S');
   70               os.write(length >> 8);
   71               os.write(length);
   72   
   73               printString(value, offset, length, os);
   74           }
   75       }
   76   
   77       public static void startReply(OutputStream os) throws IOException {
   78           os.write('r');
   79           os.write(1);
   80           os.write(0);
   81       }
   82   
   83       public static void completeReply(OutputStream os) throws IOException {
   84           os.write('z');
   85       }
   86   
   87       public static void writeFault(String code, String message, String detail, OutputStream os)
   88               throws IOException {
   89   
   90           startReply(os);
   91   
   92           os.write('f');
   93           writeString("code", os);
   94           writeString(code, os);
   95   
   96           writeString("message", os);
   97           writeString(message, os);
   98   
   99           if (detail != null) {
  100               writeString("detail", os);
  101               writeString(detail, os);
  102           }
  103           
  104           os.write('z');
  105   
  106           completeReply(os);
  107       }
  108   }

Home » synapse-1.2-src » org.apache.synapse.format.hessian » [javadoc | source]