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 Amf0Utils {
 
     public enum Type {
         NUMBER(0), BOOLEAN(1), STRING(2), OBJECT(3), MOVIE_CLIP(4), NULL(5), UNDEFINED(6), REFERENCE(7), ARRAY(8), OBJECT_END(9), STRICT_ARRAY(0x0a), DATE(0x0b), LONG_STRING(0x0c),
         UNSUPPORTED(0x0d), RECORDSET(0x0e), XML_DOCUMENT(0x0f), TYPED_OBJECT(0x10), AMF3(0x11);
 
         private final byte code;
 
         Type(int code) {
             this. = (bytecode;
         }
 
         Type(byte code) {
             this. = code;
         }
 
         public byte getCode() {
             return ;
         }
 
 
         public boolean skip(IoBufferEx in) {
             switch (this) {
             case :
             	return skipNumber(in);
             case :
             	return skipBoolean(in);
             case :
             	return skipString(in);
             case :
                 return skipObject(in);
             case :
             case :
             	// these types only have type markers, no encoded value
             	return true;
             case :
             	return false;
             case :
             	return skipArray(in);
             case :
             	return Amf3Utils.skipType(in);
             default:
                 throw new UnsupportedOperationException();
             }
         }
     }
     
     public static double getNumber(IoBufferEx bufthrows Exception {
         Amf0Utils.Type type = Amf0Utils.Type.values()[buf.get()];
         if (type != .) {
         	throw new Exception("Unxpected Type");
         }
         return buf.getDouble();
     }
 
    public static Type decodeTypeMarker(IoBufferEx buf) {
        Amf0Utils.Type type = Amf0Utils.Type.values()[buf.get()];
        return type;
    }
    
    
Decode and throw away the next Object of any type

Parameters:
in
 
     public static boolean skipType(IoBufferEx in) {
     	Amf0Utils.Type type = Amf0Utils.Type.values()[in.get()];
     	return type.skip(in);
     }
     
    private static final Charset utf8 = Charset.forName("UTF-8");
    private static final CharsetEncoder utf8encoder = .newEncoder();

    
Decode a string

Parameters:
buf
Returns:
    public static String decodeString(IoBufferEx buf) {
        int length = buf.getUnsignedShort();
        IoBufferEx slice = buf.getSlice(length);
        CharSequence s = .decode(slice.buf());
        return s.toString();
    }

    
Skip over a String

Parameters:
buf
    public static boolean skipString(IoBufferEx buf) {
        int length = buf.getUnsignedShort();
        buf.skip(length);
        return true;
    }
    
    
Decode a Number

Parameters:
buf
Returns:
    public static double decodeNumber(IoBufferEx buf) {
        return buf.getDouble();
    }
    
    
Skip over 8 bytes, the size of an AMF0 Number

Parameters:
buf
    public static boolean skipNumber(IoBufferEx buf) {
    	buf.skip(8);
    	return true;
    }
    public static boolean skipArray(IoBufferEx buf) {
        buf.skip(4); // skip int length
        while (buf.hasRemaining()) {
        	skipType(buf); // skip key
            boolean value = skipType(buf);
            // break at object end marker
            if (!value) {
                break;
            }
        }
        return true;
    }
    
    
Decode a Boolean

Parameters:
buf
Returns:
    public static boolean decodeBoolean(IoBufferEx buf) {
        return buf.get() != 0x00;
    }
    
    
Skip a Boolean

Parameters:
buf
    public static boolean skipBoolean(IoBufferEx buf) {
    	buf.skip(1);
    	return true;
    }   
    
    
Skip an AMF0 encoded ECMAScript Object.

Parameters:
buf
Returns:
    public static boolean skipObject(IoBufferEx buf) {
        while (buf.hasRemaining()) {
            skipString(buf); // skip key
            boolean value = skipType(buf);
            // break at object end marker
            if (!value) {
                break;
            } 
        }
        return true;
    }
    public static IoBufferEx encodeString(IoBufferEx bufCharSequence s) {
        buf.put(..getCode());
        return encodeStringTypeless(bufs);
    }
    
    public static IoBufferEx encodeStringTypeless(IoBufferEx bufCharSequence s) {
        try {
            buf.putPrefixedString(s, 2, );
        } catch (CharacterCodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return buf;
    }
    
    public static IoBufferEx encodeNumber(IoBufferEx bufdouble d) {
        buf.put(..getCode());
        return buf.putDouble(d);
    }
    
    public static IoBufferEx encodeUndefined(IoBufferEx buf) {
        buf.put(..getCode());
        return buf;
    }
    
    public static IoBufferEx encodeNull(IoBufferEx buf) {
        buf.put(..getCode());
        return buf;
    }
    
    public static IoBufferEx encodeBoolean(IoBufferEx bufboolean val) {
    	buf.put(..getCode());
        buf.put(val ? (byte)1 : (byte)0);
        return buf;
    }
    public static IoBufferEx encodeObjectStart(IoBufferEx buf) {
        return buf.put(..getCode());
    	
    }
    public static IoBufferEx encodeObjectEnd(IoBufferEx buf) {
    	encodeStringTypeless(buf"");
        return buf.put(..getCode());
    }
    
	public static void encodeByteArray(IoBufferEx bufIoBufferEx in) {
		Amf3Utils.encodeByteArray(bufin);
	}
	public static String decodeObjectKey(IoBufferEx in) {
		// TODO Auto-generated method stub
		return null;
	}
New to GrepCode? Check out our FAQ X