Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2007-2014 Kaazing Corporation. All rights reserved. Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 
 package org.kaazing.gateway.transport.wsr.util;
 
 
 
 public class Amf3Utils {
 
     public enum Type {
         UNDEFINED(0), NULL(1), FALSE(2), TRUE(3), INTEGER(4), DOUBLE(5), STRING(6), XMLDOC(7), DATE(8), ARRAY(9), OBJECT(10), XML(11), BYTEARRAY(12);
 
         private final byte code;
 
         Type(int code) {
             this. = (bytecode;
         }
 
         Type(byte code) {
             this. = code;
         }
 
         public byte getCode() {
             return ;
         }
         
         public boolean skip(IoBufferEx buf) {
         	switch(this) {
             case :
             case :
             case :
             case :
             	return true;
             case :
                 return skipString(buf);
             case :
             	return skipByteArray(buf);
             case :
             case :
         		default:
         			throw new UnsupportedOperationException();
         	}
         }
     }
 
     private static final Charset utf8 = Charset.forName("UTF-8");
     private static final CharsetEncoder utf8encoder = .newEncoder();

    
Decode and throw away the next Object of any type

Parameters:
buf
 
     public static boolean skipType(IoBufferEx buf) {
     	Type type = Type.values()[buf.get()];
     	return type.skip(buf);
     }
    
    
Decode variable-length U29 values

Parameters:
buf
Returns:
 
     public static int decodeLength(IoBufferEx buf) {
     	int l = 0;
     	int width = 1;
     	byte b;
     	while (width <= 4) {
     		b = buf.get();
     		if (width < 4) {
     			l <<= 7;
     			l += (b & 0x7f);
     			
     			// check for continue bit if this is not the last byte
    			if (b >> 7 == 0) {
    				break;
    			}
    		} else {
    			// use all 8 bits from the 4th byte
    			l <<= 7;
    			l += b;
    		}
    		width++;
    	}
    	return l;
    }
    
    public static int decodeLengthWithLowFlag(IoBufferEx buf) {
    	int l = decodeLength(buf);
    	// remove least significant bit
    	l >>= 1;
    	return l;
    }
    
    public static IoBufferEx encodeLength(IoBufferEx bufint l) {
    	WsUtils.encodeLength(buf.buf(), l);
    	return buf;
    }
    
    public static IoBufferEx encodeLengthWithLowFlag(IoBufferEx bufint l) {
    	// shift up 1, and set the lowest bit without
    	// altering the U29 encoded value
    	int n = (l<<1) | 1;
    	return encodeLength(bufn); 
    }
    
    public static CharSequence decodeString(IoBufferEx buf) {
        int length = decodeLength(buf);
        IoBufferEx slice = buf.getSlice(length);
        CharSequence s = .decode(slice.buf());
        return s;
    }
    
    
Skip over a String

Parameters:
buf
Returns:
    public static boolean skipString(IoBufferEx buf) {
        int length = decodeLength(buf);
        buf.skip(length);
        return true;
    }
    
    public static IoBufferEx decodeByteArray(IoBufferEx buf) {
        int length = decodeLengthWithLowFlag(buf);
        IoBufferEx slice = buf.getSlice(length);
        return slice;
    }
    
    
Skip over a ByteArray

Parameters:
buf
Returns:
    public static boolean skipByteArray(IoBufferEx buf) {
        int length = decodeLengthWithLowFlag(buf);
        buf.skip(length);
        return true;
    }
   
    public static IoBuffer encodeString(IoBuffer bufCharSequence s) {
        buf.put(..getCode());
        return encodeStringTypeless(bufs);
    }
    public static IoBuffer encodeStringTypeless(IoBuffer bufCharSequence s) {
        // advance position by two to leave room for U16 length
        int lengthAt = buf.position();
        buf.position(lengthAt + 2);
        try {
            buf.putString(s);
        } catch (CharacterCodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // write string length
        buf.putShort(lengthAt, (short) (buf.position() - (lengthAt + 2)));
        return buf;
    }
    public static IoBuffer encodeDouble(IoBuffer bufdouble n) {
        buf.put(..getCode());
        buf.putDouble(n);
        return buf;
    }
        
    public static IoBuffer encodeUndefined(IoBuffer buf) {
        buf.put(..getCode());
        return buf;
    }
    
    public static IoBuffer encodeNull(IoBuffer buf) {
        buf.put(..getCode());
        return buf;
    }
    
    public static IoBuffer encodeBoolean(IoBuffer bufboolean val) {
        buf.put(val ? ..getCode() : ..getCode());
        return buf;
    }
	public static IoBufferEx encodeByteArray(IoBufferEx bufIoBufferEx b) {
        buf.put(..getCode());
		int length = b.limit() - b.position();
		buf.put(b);
		return buf;
	}
New to GrepCode? Check out our FAQ X