Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DonAbney/37074d7ce2c1864518d110edcdb47cff to your computer and use it in GitHub Desktop.
Save DonAbney/37074d7ce2c1864518d110edcdb47cff to your computer and use it in GitHub Desktop.
code review exercise for PCA 2014
package com.somecompany.app.process;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import javax.imageio.ImageIO;
public class PersonPictureProcess extends APPProcess {
public static byte[] getImage(String id) throws Exception {
try {
byte[] imageData = null;
String filePath = System.getProperty("app.imageRepository")
+ getFrameworkContext().getPrincipal().getTenant().getId()
+ "/" + id + ".png";
File file = new File(filePath);
if (file.exists()) {
BufferedImage image = ImageIO.read(new File(filePath));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
imageData = baos.toByteArray();
}
return imageData;
} catch (Exception e) {
throw e;
}
}
public static void setImage(String id, InputStream inputStream,
String mimeSubType) throws Exception {
try {
String filePath = System.getProperty("app.imageRepository")
+ getFrameworkContext().getPrincipal().getTenant().getId()
+ "/" + id + "." + mimeSubType;
File file = new File(filePath);
file.getParentFile().mkdirs();
OutputStream outpuStream = new FileOutputStream(new File(filePath));
int read = 0;
byte[] bytes = new byte[1024];
outpuStream = new FileOutputStream(new File(filePath));
while ((read = inputStream.read(bytes)) != -1) {
outpuStream.write(bytes, 0, read);
}
outpuStream.flush();
outpuStream.close();
} catch (Exception e) {
throw e;
}
}
public static void deleteImage(String id) throws Exception {
try {
String filePath = System.getProperty("app.imageRepository")
+ getFrameworkContext().getPrincipal().getTenant().getId()
+ "/" + id + ".png";
File file = new File(filePath);
file.delete();
} catch (Exception e) {
throw e;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment