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

alex.mojaki.s3upload.Utils Maven / Gradle / Ivy

Go to download

Manages streaming of data to S3 without knowing the size beforehand and without keeping it all in memory or writing to disk.

There is a newer version: 2.2.4
Show newest version
package alex.mojaki.s3upload;

import java.lang.InterruptedException;
import java.lang.RuntimeException;
import java.lang.Thread;

/**
 * Miscellaneous useful functions.
 */
class Utils {

    /**
     * Lets you avoid dealing with {@code InterruptedException} as a checked exception and ensures
     * that the interrupted status of the thread is not lost.
     * Write {@code throw runtimeInterruptedException(e)} to make it clear to the
     * compiler and readers that the code stops here.
     */
    public static RuntimeException runtimeInterruptedException(InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new RuntimeException(e);
    }

    /**
     * Shortens the given string to the given length by replacing the middle with ...,
     * unless the string is already short enough or almost short enough in which case it is returned unmodified.
     */
    public static String skipMiddle(String string, int length) {
        int inputLength = string.length();
        if (inputLength < length * 1.1) {
            return string;
        }
        int sideLength = (length - 3) / 2;
        StringBuilder builder = new StringBuilder(length);
        builder.append(string, 0, sideLength);
        builder.append("...");
        builder.append(string, inputLength - sideLength, inputLength);
        return builder.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy