All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.resteasy.reactive.common.providers.serialisers.NumberMessageBodyHandler Maven / Gradle / Ivy

There is a newer version: 3.17.0.CR1
Show newest version
package org.jboss.resteasy.reactive.common.providers.serialisers;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;

import jakarta.ws.rs.WebApplicationException;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.ext.MessageBodyReader;

public class NumberMessageBodyHandler extends PrimitiveBodyHandler implements MessageBodyReader {
    public boolean isReadable(Class type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return Number.class.isAssignableFrom(type);
    }

    public Number readFrom(Class type, Type genericType, Annotation[] annotations, MediaType mediaType,
            MultivaluedMap httpHeaders, InputStream entityStream)
            throws IOException, WebApplicationException {
        return doReadFrom(type, entityStream);
    }

    protected Number doReadFrom(Class type, InputStream entityStream) throws IOException {
        String text = readFrom(entityStream, false);
        // we initially had one provider per number type, but the TCK wants this Number provider to be overridable
        if ((Class) type == Byte.class || (Class) type == byte.class)
            return Byte.valueOf(text);
        if ((Class) type == Short.class || (Class) type == short.class)
            return Integer.valueOf(text);
        if ((Class) type == Integer.class || (Class) type == int.class)
            return Integer.valueOf(text);
        if ((Class) type == Long.class || (Class) type == long.class)
            return Long.valueOf(text);
        if ((Class) type == Float.class || (Class) type == float.class)
            return Float.valueOf(text);
        if ((Class) type == Double.class || (Class) type == double.class)
            return Double.valueOf(text);
        if ((Class) type == BigDecimal.class)
            return new BigDecimal(text);
        if ((Class) type == BigInteger.class)
            return new BigInteger(text);
        throw new RuntimeException("Don't know how to handle number class " + type);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy