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

io.bdeploy.jersey.JerseyStreamingHelper Maven / Gradle / Ivy

Go to download

Public API including dependencies, ready to be used for integrations and plugins.

There is a newer version: 7.4.0
Show newest version
package io.bdeploy.jersey;

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

/**
 * Handles reading/writing of large data streams while tracking progress.
 */
public class JerseyStreamingHelper {

    public enum StreamDirection {
        READ,
        WRITE
    }

    public static void streamWithProgress(StreamDirection direction, InputStream in, OutputStream out, long length)
            throws IOException {

        // consume no more than length
        long remaining = length <= 0 ? Long.MAX_VALUE : length;
        final byte[] buffer = new byte[1024 * 8];
        while (remaining > 0) {
            int l = in.read(buffer, 0, (int) Math.min(1024 * 8l, remaining));
            if (l == -1) {
                break;
            }
            out.write(buffer, 0, l);
            remaining -= l;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy