@Test public void testGetBytesFromFile() throws IOException { File file=File.createTempFile("JUnit_", ".garbage"); file.createNewFile(); byte[] bs=PictureUploadUtil.getBytesFromFile(file); assertEquals(0, bs.length); FileOutputStream fos = new FileOutputStream(file); BufferedOutputStream bos = new BufferedOutputStream(fos); bos.write(new byte[]{0x11, 0x12}); bos.flush(); bos.close(); fos.flush(); fos.close(); bs=PictureUploadUtil.getBytesFromFile(file); assertEquals(2, bs.length); file.delete(); }
public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); long length = file.length(); if (length > Integer.MAX_VALUE) { throw new IOException("File too big"); } byte[] bytes = new byte[(int) length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } if (offset < bytes.length) { throw new IOException("Could not completely read file " + file.getName()); } is.close(); return bytes; }
PictureUploadUtil { public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); long length = file.length(); if (length > Integer.MAX_VALUE) { throw new IOException("File too big"); } byte[] bytes = new byte[(int) length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } if (offset < bytes.length) { throw new IOException("Could not completely read file " + file.getName()); } is.close(); return bytes; } }
PictureUploadUtil { public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); long length = file.length(); if (length > Integer.MAX_VALUE) { throw new IOException("File too big"); } byte[] bytes = new byte[(int) length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } if (offset < bytes.length) { throw new IOException("Could not completely read file " + file.getName()); } is.close(); return bytes; } PictureUploadUtil(JComponent parentComponent, final String titleBarText, final Integer unitId, final Integer viewComponentId); }
PictureUploadUtil { public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); long length = file.length(); if (length > Integer.MAX_VALUE) { throw new IOException("File too big"); } byte[] bytes = new byte[(int) length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } if (offset < bytes.length) { throw new IOException("Could not completely read file " + file.getName()); } is.close(); return bytes; } PictureUploadUtil(JComponent parentComponent, final String titleBarText, final Integer unitId, final Integer viewComponentId); void show(); void uploadFiles(File[] files, File localUploadDir); static byte[] getBytesFromFile(File file); static ByteArrayOutputStream manipulateImage(Image image); abstract void pictureExistsAction(int i); abstract void pictureSelectedAction(byte[] thumbnail, byte[] picture, String mimeType, String pictureName); abstract int findExistingPicture(String fileName); Integer getUnitId(); void setUnitId(Integer unitId); Integer getViewComponentId(); void setViewComponentId(Integer viewComponentId); }
PictureUploadUtil { public static byte[] getBytesFromFile(File file) throws IOException { InputStream is = new FileInputStream(file); long length = file.length(); if (length > Integer.MAX_VALUE) { throw new IOException("File too big"); } byte[] bytes = new byte[(int) length]; int offset = 0; int numRead = 0; while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) { offset += numRead; } if (offset < bytes.length) { throw new IOException("Could not completely read file " + file.getName()); } is.close(); return bytes; } PictureUploadUtil(JComponent parentComponent, final String titleBarText, final Integer unitId, final Integer viewComponentId); void show(); void uploadFiles(File[] files, File localUploadDir); static byte[] getBytesFromFile(File file); static ByteArrayOutputStream manipulateImage(Image image); abstract void pictureExistsAction(int i); abstract void pictureSelectedAction(byte[] thumbnail, byte[] picture, String mimeType, String pictureName); abstract int findExistingPicture(String fileName); Integer getUnitId(); void setUnitId(Integer unitId); Integer getViewComponentId(); void setViewComponentId(Integer viewComponentId);  }