net.dv8tion.jda.api.utils.data.etf.ExTermDecoder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JDA Show documentation
Show all versions of JDA Show documentation
Java wrapper for the popular chat & VOIP service: Discord https://discord.com
The newest version!
/*
* 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 javax.annotation.Nonnull;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.zip.InflaterOutputStream;
import static net.dv8tion.jda.api.utils.data.etf.ExTermTag.*;
/**
* Decodes an ETF encoded payload to a java object representation.
*
* @see #unpack(ByteBuffer)
* @see #unpackMap(ByteBuffer)
* @see #unpackList(ByteBuffer)
*
* @since 4.2.1
*/
public class ExTermDecoder
{
/**
* Unpacks the provided term into a java object.
*
* The mapping is as follows:
*
* - {@code Small Int | Int -> Integer}
* - {@code Small BigInt -> Long}
* - {@code Float | New Float -> Double}
* - {@code Small Atom | Atom -> Boolean | null | String}
* - {@code Binary | String -> String}
* - {@code List | NIL -> List}
* - {@code Map -> Map}
*
*
* @param buffer
* The {@link ByteBuffer} containing the encoded term
*
* @throws IllegalArgumentException
* If the buffer does not start with the version byte {@code 131} or contains an unsupported tag
*
* @return The java object
*/
@Nonnull
public static Object unpack(ByteBuffer buffer)
{
if (buffer.get() != -125)
throw new IllegalArgumentException("Failed header check");
return unpack0(buffer);
}
/**
* Unpacks the provided term into a java {@link Map}.
*
* The mapping is as follows:
*
* - {@code Small Int | Int -> Integer}
* - {@code Small BigInt -> Long}
* - {@code Float | New Float -> Double}
* - {@code Small Atom | Atom -> Boolean | null | String}
* - {@code Binary | String -> String}
* - {@code List | NIL -> List}
* - {@code Map -> Map}
*
*
* @param buffer
* The {@link ByteBuffer} containing the encoded term
*
* @throws IllegalArgumentException
* If the buffer does not start with a Map term, does not have the right version byte, or the format includes an unsupported tag
*
* @return The parsed {@link Map} instance
*/
@Nonnull
@SuppressWarnings("unchecked")
public static Map unpackMap(ByteBuffer buffer)
{
byte tag = buffer.get(1);
if (tag != MAP)
throw new IllegalArgumentException("Cannot unpack map from tag " + tag);
return (Map) unpack(buffer);
}
/**
* Unpacks the provided term into a java {@link List}.
*
* The mapping is as follows:
*
* - {@code Small Int | Int -> Integer}
* - {@code Small BigInt -> Long}
* - {@code Float | New Float -> Double}
* - {@code Small Atom | Atom -> Boolean | null | String}
* - {@code Binary | String -> String}
* - {@code List | NIL -> List}
* - {@code Map -> Map}
*
*
* @param buffer
* The {@link ByteBuffer} containing the encoded term
*
* @throws IllegalArgumentException
* If the buffer does not start with a List or NIL term, does not have the right version byte, or the format includes an unsupported tag
*
* @return The parsed {@link List} instance
*/
@Nonnull
@SuppressWarnings("unchecked")
public static List