File Outputstream에서 압축, 암호화 및 체크섬을 수행하는 코드가 있습니다. 다른 개체와 "OS"변수를 내가 압축 등의 암호화에 대한 "OS"개체를 포장하고 여기에서 볼 수 있으며, 다음-할당 다시 같이 코드 -여러 출력 스트림 객체를 래핑 할 때 Java Outputstream 동작
private void start() {
OutputStream os = null;
try {
os = new FileOutputStream("/some/file");
os = wrapAllRequiredTransforms(os);
//Write to os
} finally {
os.close();
}
}
private wrapAllRequiredTransforms(OutputStream os) {
if(checkSumRequired) {
os = wrapOStreamWithCheckSum(os);
}
if(encryptionRequired) {
os = wrapOStreamWithCipher(os);
}
if(compressRequired) {
os = wrapOStreamWithCompress(os);
}
}
private OutputStream wrapOStreamWithCheckSum(OutputStream os) throws Exception {
os = new DigestOutputStream(os, MessageDigest.getInstance("MD5"));
return os;
}
private OutputStream wrapOStreamWithCipher(OutputStream os) throws Exception {
SecretKeySpec secretKeySpec = new SecretKeySpec(//SomeKey, encryptionAlgorithm);
Cipher cipher = Cipher.getInstance(encryptionAlgorithm);
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
return new CipherOutputStream(os, cipher);
}
private OutputStream wrapOStreamWithCompress(OutputStream os) throws Exception {
return new GZIPOutputStream(os);
}
입니다 다음 (새 사용하여 작성) wrapOStreamWithCheckSum, wrapOStreamWithCipher 및 wrapOStreamWithCompress 메서드 내부에 있습니다. 나는 이것이 메모리 누수로 이어질 지 궁금했다. 이전에 생성 된 "os"객체는 실제로 어떻게됩니까? 다시 말하면, "new"를 사용하여 생성 된 4 개의 객체가 있지만 동일한 "os"변수에 다시 할당됩니다. 나는 새로운 객체 생성/기능 자체가 오래된 객체에 내부적으로 의존하기 때문에 이해하기가 어렵다.
감사합니다,
-Keshav
(예 : try/finally가 손상되었습니다. 파일이 발견되지 않으면 어떻게됩니까?) (acquire(); try {use();} finally {release();}'. :'final OutputStream rawOut = new FileOutputStream ("/ some/file /"); 시도 {final OutputStream out = 줄 바꿈 (rawOut); ...} 마지막 {rawOut.close();}'. –
감사합니다. Tom! 네, 맞습니다. 오류 조건은 여기에서 처리되지 않습니다. (위의 코드는 프로그램에 복사 할 때 컴파일되지 않습니다.) 위의 코드 단편은 내가 이해하려고하는 문제 만 설명하려고 시도합니다. :) – Keshav