📄 maildb.java
字号:
if (DEBUG) { System.out.println("body part content loaded"); } return body; } /** * By this method we get the real content a body part. Can be used by a class that displays mails * @param dbFileName * @param recordID * @return * @throws MyException * * @see RMSStorage */ static String loadFragmentOfBodypartContent(String dbFileName, int recordID) throws MyException { String body = null; if (DEBUG) { System.out.println("DEBUG MailDB.loadBodypartContent(String, int) - Loading body part content from database " + dbFileName); } RecordStore bodyRS = Functions.openRecordStore(dbFileName, true); if (DEBUG) { System.out.println("DEBUG MailDB.loadBodypartContent(String, int) - Database opened"); } // getRecord returns null if the bodypart is empty try { byte[] data = new byte[bodyRS.getRecordSize(recordID)]; bodyRS.getRecord(recordID, data, 0); DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(data)); //body = new String(data, 0, data.length); // TODO: this does not read unicode (i.e. czech diacritics) correctly!! body = inputStream.readUTF(); //TODO: here is an EOF while reading HTML attachment if (DEBUG) { System.out.println("DEBUG MailDB.loadBodypartContent(String, int) - loadBodypartContent body='" + body + "'"); } inputStream.close(); data = null; } catch (NullPointerException npex) { System.out.println("DEBUG MailDB.loadBodypartContent(String, int) - null pointer exception"); body = ""; } catch (Exception ex) { System.out.println("DEBUG MailDB.loadBodypartContent(String, int) - exception "); ex.printStackTrace(); throw new MyException(MyException.DB_CANNOT_LOAD_BODY); } finally { Functions.closeRecordStore(bodyRS); } if (DEBUG) { System.out.println("DEBUG MailDB.loadBodypartContent(String, int) - body part content loaded"); } return body; } private static final String NULL_STRING = "\\"; /** * This method serves as translator for string values. * If value of string is null, it is replaced with "\\" (one backslash). * If value is not null and first character is backslash this backslash * is doubled otherwise original string is returned. * * @param str string to be translated * @return escaped string */ public static String saveNullable( final String str ) { if ( str == null ) { return NULL_STRING; } final int length = str.length(); if ( length == 0 ) { return ""; } else { // if first char is backslash if ( str.charAt( 0 ) == '\\' ) { // add one backslash at the beginning return "\\" + str; } else { // return original String return str; } } } /** * Oposite for {@link #saveNullable(String)} method. * */ public static String loadNullable( final String str ) { if ( NULL_STRING.equals( str ) ) { return null; } final int length = str.length(); if ( length == 0 ) { return ""; } else { final char c1 = str.charAt( 0 ); if ( c1 == '\\' ) { return str.substring( 1 ); } else { return str; } } } /** * Handles the situation when message header cannot be saved. * * @throws MyException if the header was not saved and cannot be saved now * @throws Exception if there was exception while saving the header */ private RecordStore handleProblemWithSavingHeader(final MessageHeader header, RecordStore headerRS) throws Exception { if (Settings.deleteMailsWhenHeaderDBIsFull) { // delete some and than try to save the header again headerRS.closeRecordStore(); header.getBox().deleteOldestMails(NUM_HEADERS_TO_DELETE_IF_DB_FULL);// headerRS = Functions.openRecordStore(dbName + "_H", true);// saveHeader(headerRS, header);// throw new MyException(MyException.DB_CANNOT_SAVE_HEADER); } else { // ask user whether delete some mails. DeleteOldMails deleteOldMailsAndSaveHeader = new DeleteOldMails(headerRS, header); OKCancelDialog dialog = new OKCancelDialog("Not enough space in database", "There is not enough space to store header of this mail. Do you want to delete " + NUM_HEADERS_TO_DELETE_IF_DB_FULL + " oldest mails?", deleteOldMailsAndSaveHeader); dialog.showScreen(StartupModes.IN_NEW_THREAD); throw new MyException(MyException.DB_CANNOT_SAVE_HEADER); } //return headerRS; } private class DeleteOldMails implements Callback {// private final RecordStore headerRS; private final MessageHeader messageHeader; public DeleteOldMails(RecordStore headerRS, MessageHeader messageHeader) {// this.headerRS = headerRS; this.messageHeader = messageHeader; } public void callback(Object called, Object message) { messageHeader.getBox().deleteOldestMails(NUM_HEADERS_TO_DELETE_IF_DB_FULL);// try {// saveHeader(headerRS, messageHeader);// } catch (Exception ex2) {// throw new RuntimeException();// } } } /** * Saves the header of the message and header of all bodyparts to the RMS database. * Does not save the content of the message. * If the status of the message is header.DBStatus == MessageHeader.STORED * saves the header to existing record in the database (just updates it) * @param header the header of the message which will be saved * @return the record ID under which the header is saved * @throws mujmail.MyException */ public int saveHeader(final MessageHeader header) throws MyException { if (DEBUG) { System.out.println("DEBUG MailDB.saveHeader(MessageHeader) - saving header: " + header); } RecordStore headerRS = Functions.openRecordStore(dbName + "_H", true); if (DEBUG) { System.out.println("DEBUG MailDB.saveHeader(MessageHeader) - to database: " + this.dbName); } try { if (headerRS.getSizeAvailable() <= FREE_SPACE_IN_HEADER_DB_WHEN_DELETE_HEADERS) { //if ( header.getBox().storage.getSize() >= 3 ) { headerRS = handleProblemWithSavingHeader(header, headerRS); } else { saveHeader(headerRS, header); } } catch (MyException myex) { // cannot recover from this myex.printStackTrace(); throw myex; } catch (Exception ex) { // try to recover ex.printStackTrace(); try { headerRS = handleProblemWithSavingHeader(header, headerRS); } catch (Exception ex1) { ex1.printStackTrace(); } } finally { try { if (DEBUG) System.out.println( "DEBUG MailDB.saveHeader(MessageHeader) - Record store size = " + headerRS.getRecordSize(header.getRecordID()) ); } catch (RecordStoreNotOpenException ex) { ex.printStackTrace(); } catch (InvalidRecordIDException ex) { ex.printStackTrace(); } catch (RecordStoreException ex) { ex.printStackTrace(); } Functions.closeRecordStore(headerRS); } if (DEBUG) { System.out.println("DEBUG MailDB.saveHeader(MessageHeader) - header saved"); } return header.getRecordID(); } //its static called by a class that displays mails, therefor its static //to let user markAsDeleted a attachment from a mail public static void deleteStorageContent(String dbFileName, int recordID) throws MyException { if (DEBUG) { System.out.println("Deleting body part"); } RecordStore bodyRecordStore = Functions.openRecordStore(dbFileName, true); try { bodyRecordStore.deleteRecord(recordID); } catch (Exception ex) { throw new MyException(MyException.DB_CANNOT_DEL_BODY); } finally { Functions.closeRecordStore(bodyRecordStore); } if (DEBUG) { System.out.println("Body part deleted"); } } public static int bodyRecordSize(String dbFileName, int recordID) { RecordStore store = null; int size = -1; try { store = RecordStore.openRecordStore(dbFileName, true); size = store.getRecordSize(recordID); } catch (Exception ex) { } Functions.closeRecordStore(store); return size; } /** * Get space in bytes that database take place in persistent storage. * @return Size of database. */ public int getOccupiedSpace() { RecordStore db = null; int size = 0; try { // Headers db = RecordStore.openRecordStore(dbName + "_H", true); size += db.getSize(); db.closeRecordStore(); // Bodies db = RecordStore.openRecordStore(dbName, true); size += db.getSize(); db.closeRecordStore(); } catch (Exception ex) {} // Non existent database return size; } /** * This method loads a Vector of headers from the <code>RecordStore</code> with name nameRs. */ private void loadHeaders(MailDBTask progress) throws MyException { if (DEBUG) { System.out.println("DEBUG MailDB.loadHeaders() - start - " + dbName); } RecordStore headerRS = Functions.openRecordStore(dbName + "_H", true); if (DEBUG) { System.out.println("DEBUG MailDB.loadHeaders() - Record box opened"); } try { if (DEBUG) { System.out.println("DEBUG MailDB.loadHeaders() - number of records: " + headerRS.getNumRecords()); } if (headerRS.getNumRecords() > 0) { RecordEnumeration enumeration = headerRS.enumerateRecords(null, null, false); byte[] data = new byte[250]; DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(data)); int recordsNumber = enumeration.numRecords(); byte bodyPartsCount; int id, sizeOfRecord; progress.setTitle(Lang.get(Lang.ALRT_LOADING) + progress.actionInvoker.getName()); progress.updateProgress(recordsNumber, 0); //hdrRefer.ensureCapacity(numRcds);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -