@Test public void testCopyIntoTempFile() throws IOException { ByteArrayInputStream bis = new ByteArrayInputStream("foo".getBytes()); File file = TempUtils.copyIntoTempFile("bar", bis); Assert.assertTrue(file.exists()); Assert.assertTrue(file.isFile()); Assert.assertTrue(file.getName().contains("bar")); Assert.assertEquals(bis.available(), 0); ByteArrayOutputStream bos = new ByteArrayOutputStream(); FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[8192]; int length; while ((length = fis.read(buffer)) > 0) { bos.write(buffer, 0, length); } fis.close(); bos.close(); Assert.assertEquals(bos.toByteArray(), "foo".getBytes()); }
public static File copyIntoTempFile(String fileName, InputStream inputStream) throws IOException { BufferedInputStream bufferedStream = inputStream instanceof BufferedInputStream ? (BufferedInputStream) inputStream : new BufferedInputStream(inputStream); File destFile = null; try { destFile = File.createTempFile(fileName, null); destFile.deleteOnExit(); FileOutputStream fileOutputStream = new FileOutputStream(destFile); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); try { byte[] buffer = new byte[8192]; int length; while ((length = bufferedStream.read(buffer)) > 0) { bufferedOutputStream.write(buffer, 0, length); } } finally { bufferedOutputStream.close(); fileOutputStream.close(); } } finally { bufferedStream.close(); inputStream.close(); } return destFile; }
TempUtils { public static File copyIntoTempFile(String fileName, InputStream inputStream) throws IOException { BufferedInputStream bufferedStream = inputStream instanceof BufferedInputStream ? (BufferedInputStream) inputStream : new BufferedInputStream(inputStream); File destFile = null; try { destFile = File.createTempFile(fileName, null); destFile.deleteOnExit(); FileOutputStream fileOutputStream = new FileOutputStream(destFile); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); try { byte[] buffer = new byte[8192]; int length; while ((length = bufferedStream.read(buffer)) > 0) { bufferedOutputStream.write(buffer, 0, length); } } finally { bufferedOutputStream.close(); fileOutputStream.close(); } } finally { bufferedStream.close(); inputStream.close(); } return destFile; } }
TempUtils { public static File copyIntoTempFile(String fileName, InputStream inputStream) throws IOException { BufferedInputStream bufferedStream = inputStream instanceof BufferedInputStream ? (BufferedInputStream) inputStream : new BufferedInputStream(inputStream); File destFile = null; try { destFile = File.createTempFile(fileName, null); destFile.deleteOnExit(); FileOutputStream fileOutputStream = new FileOutputStream(destFile); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); try { byte[] buffer = new byte[8192]; int length; while ((length = bufferedStream.read(buffer)) > 0) { bufferedOutputStream.write(buffer, 0, length); } } finally { bufferedOutputStream.close(); fileOutputStream.close(); } } finally { bufferedStream.close(); inputStream.close(); } return destFile; } private  TempUtils(); }
TempUtils { public static File copyIntoTempFile(String fileName, InputStream inputStream) throws IOException { BufferedInputStream bufferedStream = inputStream instanceof BufferedInputStream ? (BufferedInputStream) inputStream : new BufferedInputStream(inputStream); File destFile = null; try { destFile = File.createTempFile(fileName, null); destFile.deleteOnExit(); FileOutputStream fileOutputStream = new FileOutputStream(destFile); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); try { byte[] buffer = new byte[8192]; int length; while ((length = bufferedStream.read(buffer)) > 0) { bufferedOutputStream.write(buffer, 0, length); } } finally { bufferedOutputStream.close(); fileOutputStream.close(); } } finally { bufferedStream.close(); inputStream.close(); } return destFile; } private  TempUtils(); static File createTempDir(String prefix); static File copyIntoTempFile(String fileName, InputStream inputStream); }
TempUtils { public static File copyIntoTempFile(String fileName, InputStream inputStream) throws IOException { BufferedInputStream bufferedStream = inputStream instanceof BufferedInputStream ? (BufferedInputStream) inputStream : new BufferedInputStream(inputStream); File destFile = null; try { destFile = File.createTempFile(fileName, null); destFile.deleteOnExit(); FileOutputStream fileOutputStream = new FileOutputStream(destFile); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); try { byte[] buffer = new byte[8192]; int length; while ((length = bufferedStream.read(buffer)) > 0) { bufferedOutputStream.write(buffer, 0, length); } } finally { bufferedOutputStream.close(); fileOutputStream.close(); } } finally { bufferedStream.close(); inputStream.close(); } return destFile; } private  TempUtils(); static File createTempDir(String prefix); static File copyIntoTempFile(String fileName, InputStream inputStream);  }