📄 rmsstorage.java
字号:
handleProblemWithSavingBodyPart(saveable); saveContentToDB(decodedBodyBytes, content, safeMode); } return saveable; } /** * Deletes the conten of this storage. */ public void deleteContent() { try { MailDB.deleteStorageContent(contentStorage.DBFileName, recordID); } catch (MyException ex) { ex.printStackTrace(); } } /** * Load this object from given input stream (typically rms database). * @param inputStream input stream that contains data of this object. * @throws java.lang.Exception */ public void load(DataInputStream inputStream) throws Exception { // load content part recordID = inputStream.readInt(); } /** * Saves this object to given output stream (typycally rms database). * @param outputStream output stream to that save the data of this object. * @throws java.lang.Exception */ public void save(DataOutputStream outputStream) throws Exception { outputStream.writeInt(getRecordID()); } private void saveContentToDB(String content, boolean safeMode) throws MyException { recordID = getBodyPart().getBox().getMailDB().saveFragmentBodypartContent(content, safeMode); updateDBFileName(safeMode); } /** * Updates the database file name. The name of database file name * must be the same for all bodyparts. * @param safeMode */ private void updateDBFileName(boolean safeMode) { String newDBFileName = safeMode ? MailDB.safeModeDBFile : getBodyPart().getBox().getDBFileName(); if (contentStorage.DBFileName != null) { // check if the name of the database is correct: the same if (!contentStorage.DBFileName.equals(newDBFileName)) { throw new RuntimeException("The content of one bodypart must be saved in the same database."); } } else { // set the database name contentStorage.DBFileName = newDBFileName; } } private void saveContentToDB(byte[] decodedBodyBytes, byte[] content, boolean safeMode) throws MyException { recordID = getBodyPart().getBox().getMailDB().saveFragmentOfBodypartContent(decodedBodyBytes != null ? decodedBodyBytes : content, safeMode); updateDBFileName(safeMode); } } public RMSStorage(BodyPart bodyPart) { super(bodyPart); rmsStorageParts = new RMSStorageParts(this); } /** * Initialize class by copying another instance. * Note that if the mode is SHALLOW_COPY it the body part which content * will is stored in this ContentStorage should be in the same box as original * storage. * @param bp the body part which content is stored in this storage * @param copy AttachmentPart instance to copy * @param copyMode defines copying mode */ private RMSStorage(BodyPart bp, RMSStorage content, CopyingModes copyMode) { super(bp); if (copyMode == CopyingModes.NO_COPY) { // the instance is yet created rmsStorageParts = new RMSStorageParts(this); return; } if (copyMode == CopyingModes.SHALLOW_COPY) { // TODO: copy rms storage parts? this.DBFileName = content.DBFileName; this.rmsStorageParts = content.rmsStorageParts; this.setSize(content.getSize()); return; } if (copyMode == CopyingModes.DEEP_COPY) { rmsStorageParts = new RMSStorageParts(this); addContent(content, false); return; } } public boolean willReturnFirstContent() { return (numPartToGet == 0); } /** * Tryes to get maximum content parts beginning with content part with number * numPartToGet. * Gets content parts until it is reached the end of this content or it is * not enough memory. * @return the stirng with content. */ private String getMaximumContent() throws Throwable { StringBuffer sb = new StringBuffer(); String lastAddedContent = null; for (int i = numPartToGet; i < rmsStorageParts.getNumParts(); i++) { try { lastAddedContent = rmsStorageParts.getStoragePart(i).getContent(); sb.append(lastAddedContent); incNumPartToGet(); } catch (Throwable exception) { // TODO: show message to user: Load body part failed or something // like that? rewindStringbuffer(sb, lastAddedContent); throw exception; } } return sb.toString(); } /** * Counterpart of getMaximumContent for binary data. * @return the stirng with content * @see getMaximumContent */ private byte[] getMaximumContentRaw() { ByteArrayOutputStream bos = new ByteArrayOutputStream(); System.out.println("DEBUG - RMSStorage.getMaximumContentRaw: rmsStorageParts.getNumParts() = " + rmsStorageParts.getNumParts()); for (int i = numPartToGet; i < rmsStorageParts.getNumParts(); i++) { try { byte[] lastAddedContent = rmsStorageParts.getStoragePart(i).getContentRaw(); bos.write(lastAddedContent); incNumPartToGet(); } catch (Throwable exception) { exception.printStackTrace(); break; } } return bos.toByteArray(); } /** * Increases number of part to get. Called when part of the content is readed. * If it is readed the last part of the content, next part of the content * to get will be the first one. */ private void incNumPartToGet() { if (numPartToGet + 1 < rmsStorageParts.getNumParts()) { numPartToGet++; } else { numPartToGet = 0; } } private void decNumPartToGet() { if (numPartToGet > 0) numPartToGet--; } public void resetToFirstContent() { numPartToGet = 0; } public StorageTypes getStorageType() { return StorageTypes.RMS_STORAGE; } public ContentStorage copy(BodyPart bp, CopyingModes copyMode) { if (!checkCopy(bp, copyMode)) { return null; } return new RMSStorage(bp, this, copyMode); } public void saveStorageHeader(DataOutputStream outputStream) throws Exception { super.saveStorageHeader(outputStream); outputStream.writeUTF(DBFileName); rmsStorageParts.save(outputStream); } public void loadStorage(DataInputStream inputStream) throws Exception { super.loadStorage(inputStream); DBFileName = inputStream.readUTF(); rmsStorageParts.load(inputStream); } public String sendContentToConnection(ConnectionInterface connection, MailSender.SendingModes sendingMode, boolean returnSendedData) throws Throwable { String senderData = ""; do { senderData = conditionallyAppend(senderData, sendRMSBodyPartData(connection, getContent(), sendingMode), returnSendedData); } while (!willReturnFirstContent()); return senderData; } /** * Sends data from RMS storage to connection. * * @param rmsData the data from rms storage (from method getContent() of RMSStorage) * @param capturedMailText the string that was already sent * @param sendingMode used to encode the string before sending * @return the string that was sent */ private String sendRMSBodyPartData(ConnectionInterface connection, String rmsData, MailSender.SendingModes sendingMode) throws Exception { String body = Functions.toCRLF( rmsData + (Settings.signature.length() != 0 ? "\r\n--\r\n" + Settings.signature : "")); // Sending body String tmpMailLine = sendingMode.toEncoding(body, false); connection.sendCRLF(tmpMailLine); return MailSender.captureStrCRLF("", tmpMailLine); } protected void preallocateToNextSaving() { // TODO: warning: addToContent / addToContentRaw still allocates something // see this methods nextRMSStoragePart = new RMSStoragePart(this); } protected boolean preallocatedToNextSaving() { return nextRMSStoragePart != null; } protected long addToContent(String content, boolean safeMode) throws Exception { RMSStoragePart rmsStoragePart = nextRMSStoragePart; nextRMSStoragePart = null; long wasSaved = rmsStoragePart.saveContent(content, safeMode); rmsStorageParts.addStoragePart(rmsStoragePart); return wasSaved; } protected long addToContentRaw(byte[] content, boolean safeMode) throws Exception { RMSStoragePart rmsStoragePart = nextRMSStoragePart; nextRMSStoragePart = null; long wasSaved = rmsStoragePart.saveContentRaw(content, safeMode); rmsStorageParts.addStoragePart(rmsStoragePart); return wasSaved; } public synchronized String getNotRawContent() throws Throwable { //if (getSize() == 0) { //throw new Exception("There is no content in this storage."); //} String returnString = null; try { if (DEBUG) System.out.println("DEBUG RMSStorage.getContent - geting the content."); returnString = getMaximumContent(); if (DEBUG) System.out.println("DEBUG RMSStorage.getContent - the content getted."); } catch (Throwable ex) { if (DEBUG) System.out.println("DEBUG RMSStorage.getContent - the exception while getting content."); ex.printStackTrace(); numPartToGet = 0; throw ex; } return returnString; } public synchronized byte[] getContentRaw() throws MyException { byte[] returnData = null; try { returnData = getMaximumContentRaw(); } catch (Throwable ex) { ex.printStackTrace(); System.out.println("getContentRaw: exception!!!"); numPartToGet = 0; } return returnData; } protected void deleteContentFromStorage() { rmsStorageParts.deleteContent(); } private void rewindStringbuffer(StringBuffer sb, String lastAddedContent) { if (lastAddedContent != null) { sb.delete(sb.length() - lastAddedContent.length(), sb.length()); } decNumPartToGet(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -