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

com.github.adchilds.util.FileUtil Maven / Gradle / Ivy

Go to download

A simple Jython wrapper to allow JVM-based projects to execute or evaluate Python scripts at runtime with ease.

There is a newer version: 2.0.1
Show newest version
package com.github.adchilds.util;

import java.io.*;

/**
 * Provides static file operations, such as converting a {@link String} or {@link File} to an {@link InputStream}.
 *
 * @author Adam Childs
 * @since 0.1
 */
public class FileUtil {

    /**
     * Attempts to convert the given {@code object} to an {@link InputStream}. If the object cannot be converted, throws
     * an {@link Exception}.
     *
     * Current supported object conversions include:
     * 
    *
  • {@link String} - as a fully qualified file path
  • *
  • {@link File}
  • *
* * @param object the object to convert to an input stream * @return a new {@link InputStream} created from the given {@code object} * @throws IOException when the given Object cannot be converted to a FileInputStream */ public static InputStream getFileInputStream(Object object) throws IOException { if (object instanceof String) { return new FileInputStream((String) object); } else if (object instanceof File) { return new FileInputStream((File) object); } throw new IOException("Could not convert the given object to an InputStream. object=[" + object + "]"); } /** * Reads the entire contents of the given {@link File} to a {@link String}. Defaults to the UTF-8 character encoding. * * @param file the {@link File} to read the contents of * @return a new {@link String} that contains the contents of the given file * @throws IOException when the given File cannot be found or read from */ public static String readFully(File file) throws IOException { if (file == null) { return ""; } return readFully(new FileInputStream(file), "UTF-8"); } /** * Reads the entire contents of the given {@link InputStream} to a {@link String}. * * @param inputStream the {@link InputStream} to read the contents of * @param encoding the character encoding that the resulting {@link String} should be (i.e. UTF-8) * @return a new {@link String} that contains the contents of the given stream * @throws IOException when the given InputStream cannot be read from */ public static String readFully(InputStream inputStream, String encoding) throws IOException { return new String(readFully(inputStream), encoding); } /** * Reads the entire contents of the given {@link InputStream} to a new byte array. * * @param inputStream the {@link InputStream} to read the contents of * @return a new array of bytes that contains the contents of the given stream * @throws IOException when the given InputStream cannot be read from */ private static byte[] readFully(InputStream inputStream) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int length; while ((length = inputStream.read(buffer)) != -1) { baos.write(buffer, 0, length); } return baos.toByteArray(); } // Don't allow this class to be instantiated private FileUtil() { } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy