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

com.github.davidmoten.odata.client.Util Maven / Gradle / Ivy

package com.github.davidmoten.odata.client;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

public class Util {

    public static  T nvl(T object, T ifNull) {
        if (object == null) {
            return ifNull;
        } else {
            return object;
        }
    }

    static byte[] toByteArray(InputStream in) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        byte[] buffer = new byte[8192];
        int n;
        try {
            while ((n = in.read(buffer)) != -1) {
                bytes.write(buffer, 0, n);
            }
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
        return bytes.toByteArray();
    }

    public static String utf8(InputStream in) {
        return new String(toByteArray(in), StandardCharsets.UTF_8);
    }

    public static  Stream> buffer(Stream stream, int size) {
        return StreamSupport.stream(
                Spliterators.spliteratorUnknownSize(buffer(stream.iterator(), size), 0), false);
    }

    public static  Iterator> buffer(Iterator it, int size) {
        return new Iterator>() {
            @Override
            public boolean hasNext() {
                return it.hasNext();
            }

            @Override
            public List next() {
                if (!hasNext()) {
                    throw new NoSuchElementException();
                }
                List list = new ArrayList<>();
                for (long v = 0; v < size && hasNext(); v++) {
                    list.add(it.next());
                }
                return list;
            }
        };
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy