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.
/*
* Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
*
* Licensed 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 net.dv8tion.jda.api.utils.data.etf;
import net.dv8tion.jda.api.utils.data.DataArray;
import net.dv8tion.jda.api.utils.data.SerializableData;
import javax.annotation.Nonnull;
import java.nio.Buffer;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import static net.dv8tion.jda.api.utils.data.etf.ExTermTag.*;
/**
* Encodes an object into a binary ETF representation.
*
* @see #pack(Object)
*
* @since 4.2.1
*/
public class ExTermEncoder
{
/**
* Encodes the provided object into an ETF buffer.
*
*
The mapping is as follows:
*
*
{@code String -> Binary}
*
{@code Map -> Map}
*
{@code Collection -> List | NIL}
*
{@code Byte -> Small Int}
*
{@code Integer, Short -> Int | Small Int}
*
{@code Long -> Small BigInt | Int | Small Int}
*
{@code Float, Double -> New Float}
*
{@code Boolean -> Atom(Boolean)}
*
{@code null -> Atom("nil")}
*
*
* @param data
* The object to encode
*
* @throws UnsupportedOperationException
* If there is no type mapping for the provided object
*
* @return {@link ByteBuffer} with the encoded ETF term
*/
@Nonnull
public static ByteBuffer pack(Object data)
{
ByteBuffer buffer = ByteBuffer.allocate(1024);
buffer.put((byte) 131);
ByteBuffer packed = pack(buffer, data);
// This cast prevents issues with backwards compatibility in the ABI (java 11 made breaking changes)
((Buffer) packed).flip();
return packed;
}
@Nonnull
@SuppressWarnings("unchecked")
private static ByteBuffer pack(ByteBuffer buffer, Object value)
{
if (value instanceof String)
return packBinary(buffer, (String) value);
if (value instanceof Map)
return packMap(buffer, (Map) value);
if (value instanceof SerializableData)
return packMap(buffer, ((SerializableData) value).toData().toMap());
if (value instanceof Collection)
return packList(buffer, (Collection