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

com.dtflys.forest.http.body.ResourceRequestBody Maven / Gradle / Ivy

There is a newer version: 1.6.1
Show newest version
package com.dtflys.forest.http.body;

import com.dtflys.forest.exceptions.ForestRuntimeException;
import com.dtflys.forest.http.ForestRequestBody;
import com.dtflys.forest.utils.ForestDataType;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.io.InputStream;

public class ResourceRequestBody extends ForestRequestBody {

    private Resource resource;

    public ResourceRequestBody(Resource resource) {
        this.resource = resource;
    }

    public Resource getResource() {
        return resource;
    }

    public void setResource(Resource resource) {
        this.resource = resource;
    }

    @Override
    public byte[] getByteArray() {
        try {
            InputStream inputStream = resource.getInputStream();
            return IOUtils.toByteArray(inputStream);
        } catch (IOException e) {
            throw new ForestRuntimeException(e);
        }
    }

    @Override
    public ForestDataType getDefaultBodyType() {
        return ForestDataType.BINARY;
    }

    @Override
    public ResourceRequestBody clone() {
        ResourceRequestBody newBody = new ResourceRequestBody(resource);
        newBody.setDefaultValue(getDefaultValue());
        return newBody;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy