Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
*
* 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.apache.qpid.proton.message;
import java.math.BigDecimal;
import java.nio.CharBuffer;
import java.text.DecimalFormat;
import java.util.*;
import org.apache.qpid.proton.type.DescribedType;
import org.apache.qpid.proton.type.Symbol;
import org.apache.qpid.proton.type.transport.Flow;
public class AMQPMessageFormat
{
private static final char START_LIST = '[';
private static final char DESCRIPTOR_CHAR = '@';
private static final char SYMBOL_START = ':';
private static final char QUOTE_CHAR = '"';
private static final char START_MAP = '{';
private static final char END_LIST = ']';
private static final char END_MAP = '}';
private static final char[] HEX = "0123456789abcdef".toCharArray();
public String encode(Object o)
{
if(o instanceof Boolean)
{
return encodeBoolean((Boolean)o);
}
if(o instanceof byte[])
{
return encodeBinary((byte[]) o);
}
if(o instanceof Number)
{
return encodeNumber((Number) o);
}
if(o instanceof String)
{
return encodeString((String) o);
}
if(o instanceof Symbol)
{
return encodeSymbol((Symbol) o);
}
if(o instanceof List)
{
return encodeList((List)o);
}
if(o instanceof Map)
{
return encodeMap((Map) o);
}
if(o instanceof DescribedType)
{
return encodeDescribedType((DescribedType)o);
}
if(o == null)
{
return "null";
}
return null;
}
private String encodeBinary(byte[] o)
{
StringBuilder b = new StringBuilder();
b.append("b\"");
for(byte x : o)
{
if(x >= 32 && x < 127 && x != '"' && x != '\\')
{
b.append((char)x);
}
else
{
b.append("\\x");
b.append(HEX[(x>>4) & 0x0f ]);
b.append(HEX[(x) & 0x0f ]);
}
}
b.append('"');
return b.toString();
}
private String encodeNumber(Number o)
{
if(o instanceof Float || o instanceof Double || o instanceof BigDecimal)
{
DecimalFormat df = new DecimalFormat("############.#######");
return df.format(o);
}
else
{
Formatter f = new Formatter();
return f.format("%d", o.longValue()).toString();
}
}
private String encodeBoolean(boolean o)
{
return o ? "true" : "false";
}
private String encodeDescribedType(DescribedType o)
{
StringBuilder b = new StringBuilder();
b.append(DESCRIPTOR_CHAR);
b.append(encode(o.getDescriptor()));
b.append(' ');
b.append(encode(o.getDescribed()));
return b.toString();
}
private String encodeMap(Map