1
0
mirror of https://github.com/moparisthebest/davmail synced 2024-12-13 03:02:22 -05:00

Improve exception handling

git-svn-id: http://svn.code.sf.net/p/davmail/code/trunk@48 3d1905a2-6b24-0410-a738-b14d5a86fcbd
This commit is contained in:
mguessan 2007-04-25 22:04:37 +00:00
parent d2e141e9f7
commit aad97b08b4

View File

@ -665,8 +665,9 @@ public class ExchangeSession {
StringBuffer result = new StringBuffer(); StringBuffer result = new StringBuffer();
boolean mstnefDetected = false; boolean mstnefDetected = false;
String boundary = null; String boundary = null;
BufferedReader reader = null;
try { try {
BufferedReader reader = new BufferedReader(new StringReader(fullHeaders)); reader = new BufferedReader(new StringReader(fullHeaders));
String line; String line;
line = reader.readLine(); line = reader.readLine();
while (line != null && line.length() > 0) { while (line != null && line.length() > 0) {
@ -754,13 +755,23 @@ public class ExchangeSession {
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException("Unable to preprocess headers " + e + " " + e.getMessage()); throw new RuntimeException("Unable to preprocess headers " + e + " " + e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// ignore
}
}
} }
} }
public void write(OutputStream os) throws IOException { public void write(OutputStream os) throws IOException {
// TODO : filter submessage headers in fullHeaders // TODO : filter submessage headers in fullHeaders
BufferedReader reader = new BufferedReader(new StringReader(fullHeaders)); BufferedReader reader = null;
try {
reader = new BufferedReader(new StringReader(fullHeaders));
// skip first line // skip first line
reader.readLine(); reader.readLine();
MimeHeader mimeHeader = new MimeHeader(); MimeHeader mimeHeader = new MimeHeader();
@ -781,6 +792,15 @@ public class ExchangeSession {
writeMimeMessage(reader, os, mimeHeader); writeMimeMessage(reader, os, mimeHeader);
} }
os.flush(); os.flush();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// ignore
}
}
}
} }
public void writeMimeMessage(BufferedReader reader, OutputStream os, MimeHeader mimeHeader) throws IOException { public void writeMimeMessage(BufferedReader reader, OutputStream os, MimeHeader mimeHeader) throws IOException {
@ -852,8 +872,8 @@ public class ExchangeSession {
} }
protected void writeAttachment(OutputStream os, MimeHeader mimeHeader, Attachment attachment) throws IOException { protected void writeAttachment(OutputStream os, MimeHeader mimeHeader, Attachment attachment) throws IOException {
OutputStream quotedOs = null;
try { try {
OutputStream quotedOs;
try { try {
// try another base64Encoder implementation // try another base64Encoder implementation
if ("base64".equalsIgnoreCase(mimeHeader.contentTransferEncoding)) { if ("base64".equalsIgnoreCase(mimeHeader.contentTransferEncoding)) {
@ -896,12 +916,23 @@ public class ExchangeSession {
wdr.retrieveSessionInstance().executeMethod(method); wdr.retrieveSessionInstance().executeMethod(method);
// encode attachment // encode attachment
BufferedInputStream bis = new BufferedInputStream(method.getResponseBodyAsStream()); BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(method.getResponseBodyAsStream());
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
int count; int count;
while ((count = bis.read(buffer)) >= 0) { while ((count = bis.read(buffer)) >= 0) {
quotedOs.write(buffer, 0, count); quotedOs.write(buffer, 0, count);
} }
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// ignore
}
}
}
bis.close(); bis.close();
quotedOs.flush(); quotedOs.flush();
os.write('\r'); os.write('\r');
@ -910,6 +941,14 @@ public class ExchangeSession {
} catch (HttpException e) { } catch (HttpException e) {
throw new IOException(e + " " + e.getMessage()); throw new IOException(e + " " + e.getMessage());
} finally {
if (quotedOs != null) {
try {
quotedOs.close();
} catch (IOException e) {
// ignore
}
}
} }
} }
@ -1009,7 +1048,9 @@ public class ExchangeSession {
public void printHeaders(OutputStream os) throws IOException { public void printHeaders(OutputStream os) throws IOException {
String line; String line;
BufferedReader reader = new BufferedReader(new StringReader(fullHeaders)); BufferedReader reader = null;
try {
reader = new BufferedReader(new StringReader(fullHeaders));
// skip first line // skip first line
reader.readLine(); reader.readLine();
line = reader.readLine(); line = reader.readLine();
@ -1019,6 +1060,15 @@ public class ExchangeSession {
os.write('\n'); os.write('\n');
line = reader.readLine(); line = reader.readLine();
} }
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// ignore
}
}
}
} }
/** /**