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

cucumber.util.FixJava Maven / Gradle / Ivy

There is a newer version: 7.18.1
Show newest version
package cucumber.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

public class FixJava {

    public static String join(List strings, String separator) {
        StringBuilder sb = new StringBuilder();
        int i = 0;
        for (String s : strings) {
            if (i != 0) sb.append(separator);
            sb.append(s);
            i++;
        }
        return sb.toString();
    }

    public static  List map(List objects, Mapper mapper) {
        List result = new ArrayList(objects.size());
        for (T o : objects) {
            result.add(mapper.map(o));
        }
        return result;
    }

    public static String readResource(String resourcePath) throws RuntimeException {
        try {
            Reader reader = new InputStreamReader(FixJava.class.getResourceAsStream(resourcePath), "UTF-8");
            return readReader(reader);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static String readReader(Reader in) throws RuntimeException {
        try {
            StringBuilder buffer = new StringBuilder();
            final char[] data = new char[0x10000];
            int read;

            while ((read = in.read(data, 0, data.length)) != -1) {
                buffer.append(data, 0, read);
            }
            return buffer.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static byte[] readStream(InputStream in) throws RuntimeException {
        try {
            ByteArrayOutputStream buffer = new ByteArrayOutputStream();
            final byte[] data = new byte[0x10000];
            int read;

            while ((read = in.read(data, 0, data.length)) != -1) {
                buffer.write(data, 0, read);
            }
            buffer.flush();
            return buffer.toByteArray();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy