/** * Gets the media data {@code byte[]} located in {@code fileName}. * * @param fileName the media file to load * @return the media data {@code byte[]} located in {@code fileName} * @throws IOException if the media could not be read */ public static byte[] getMediaDataFromFile(String fileName) throws IOException { return getMediaDataFromFile(new File(fileName)); }
/** * Gets the media data {@code byte[]} located at {@code url} or * {@code null} if the media could not be loaded. * * @param url the media URL to load * @return the media data {@code byte[]} located at {@code url} * @throws IOException if the media could not be read from the URL */ public static byte[] getMediaDataFromUrl(String url) throws IOException { return getMediaDataFromUrl(new URL(url)); }
/** * Saves media data downloaded from an API in {@code byte[]} format into a * file on the file system. * * @param mediaData the media data {@code byte[]} to store on the file system * @param fileName the name of the file on the file system to save the media * data into * @throws FileNotFoundException if the file exists but is a directory, does * not exist but cannot be created, or cannot be opened for any reason * @throws IOException if the file cannot be written to */ public static void saveMediaDataToFile(byte[] mediaData, String fileName) throws FileNotFoundException, IOException { saveMediaDataToFile(mediaData, new File(fileName)); }
/** * Gets the media data {@code byte[]} located in {@code file}. * * @param file the media file to load * @return the media data {@code byte[]} located in {@code file} * @throws IOException if the media could not be read */ public static byte[] getMediaDataFromFile(File file) throws IOException { return getByteArrayFromStream(new FileInputStream(file)); }
/** * Saves media data downloaded from an API in {@code byte[]} format into a * file on the file system. * * @param mediaData the media data {@code byte[]} to store on the file system * @param file the file on the file system to save the media data into * @throws FileNotFoundException if the file exists but is a directory, does * not exist but cannot be created, or cannot be opened for any reason * @throws IOException if the file cannot be written to */ public static void saveMediaDataToFile(byte[] mediaData, File file) throws FileNotFoundException, IOException { writeBytesToStream(mediaData, new FileOutputStream(file)); }
/** * Gets the media data {@code byte[]} located at {@code url} or * {@code null} if the media could not be loaded. * * @param url the media URL to load * @return the media data {@code byte[]} located at {@code url} * @throws IOException if the media could not be read from the URL */ public static byte[] getMediaDataFromUrl(URL url) throws IOException { return getByteArrayFromStream(url.openStream()); }
/** * Saves media data downloaded from an API in {@code byte[]} format into a * file on the file system. * * @param mediaData the media data {@code byte[]} to store on the file system * @param file the file on the file system to save the media data into * @throws FileNotFoundException if the file exists but is a directory, does * not exist but cannot be created, or cannot be opened for any reason * @throws IOException if the file cannot be written to */ public static void saveMediaDataToFile(byte[] mediaData, File file) throws FileNotFoundException, IOException { writeBytesToStream(mediaData, new FileOutputStream(file)); }
/** * Saves media data downloaded from an API in {@code byte[]} format into a * file on the file system. * * @param mediaData the media data {@code byte[]} to store on the file system * @param fileName the name of the file on the file system to save the media * data into * @throws FileNotFoundException if the file exists but is a directory, does * not exist but cannot be created, or cannot be opened for any reason * @throws IOException if the file cannot be written to */ public static void saveMediaDataToFile(byte[] mediaData, String fileName) throws FileNotFoundException, IOException { saveMediaDataToFile(mediaData, new File(fileName)); }
/** * Gets the media data {@code byte[]} located at {@code url} or * {@code null} if the media could not be loaded. * * @param url the media URL to load * @return the media data {@code byte[]} located at {@code url} * @throws IOException if the media could not be read from the URL */ public static byte[] getMediaDataFromUrl(String url) throws IOException { return getMediaDataFromUrl(new URL(url)); }
/** * Gets the media data {@code byte[]} located in {@code fileName}. * * @param fileName the media file to load * @return the media data {@code byte[]} located in {@code fileName} * @throws IOException if the media could not be read */ public static byte[] getMediaDataFromFile(String fileName) throws IOException { return getMediaDataFromFile(new File(fileName)); }
/** * Gets the media data {@code byte[]} located at {@code url} or * {@code null} if the media could not be loaded. * * @param url the media URL to load * @return the media data {@code byte[]} located at {@code url} * @throws IOException if the media could not be read from the URL */ public static byte[] getMediaDataFromUrl(URL url) throws IOException { return getByteArrayFromStream(url.openStream()); }
@Test public void testSaveMediaDataToFile_stringArg() throws IOException { File outputFile = tempFolder.newFile(); Media.saveMediaDataToFile(TEST_DATA, outputFile.getPath()); FileInputStream inputStream = new FileInputStream(outputFile); byte[] actualBytes; try { actualBytes = ByteStreams.toByteArray(inputStream); } finally { inputStream.close(); } assertArrayEquals(TEST_DATA, actualBytes); }
@Test public void testGetMediaDataFromUrl_stringArg() throws IOException { assertArrayEquals(TEST_DATA, Media.getMediaDataFromUrl(inputFile.toURI().toString())); }
@Test public void testGetMediaDataFromFile_stringArg() throws IOException { assertArrayEquals(TEST_DATA, Media.getMediaDataFromFile(inputFile.getPath())); }
/** * Gets the media data {@code byte[]} located in {@code file}. * * @param file the media file to load * @return the media data {@code byte[]} located in {@code file} * @throws IOException if the media could not be read */ public static byte[] getMediaDataFromFile(File file) throws IOException { return getByteArrayFromStream(new FileInputStream(file)); }
@Test public void testSaveMediaDataToFile_fileArg() throws IOException { File outputFile = tempFolder.newFile(); Media.saveMediaDataToFile(TEST_DATA, outputFile); FileInputStream inputStream = new FileInputStream(outputFile); byte[] actualBytes; try { actualBytes = ByteStreams.toByteArray(inputStream); } finally { inputStream.close(); } assertArrayEquals(TEST_DATA, actualBytes); } }
@Test public void testGetMediaDataFromUrl_urlArg() throws IOException { assertArrayEquals(TEST_DATA, Media.getMediaDataFromUrl(inputFile.toURI().toURL())); }
@Test public void testGetMediaDataFromFile_fileArg() throws IOException { assertArrayEquals(TEST_DATA, Media.getMediaDataFromFile(inputFile)); }