-
-
Save DonAbney/37074d7ce2c1864518d110edcdb47cff to your computer and use it in GitHub Desktop.
code review exercise for PCA 2014
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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