/** * データ量を取得 * * @param topic * @param category * @return */ public int calcMessageDataSize(String topic, String category) { // メモリ上に存在している場合は,コメントをメモリ上から取得 SortedSet<CommentElement> comments = localTopic.get(topic, category); // StringBuffer commentBuf = new StringBuffer("<topic>\n"); if (comments == null) { // コメントをファイルから持ってくる try { loadTopicFromFile(topic, category); comments = localTopic.get(topic, category); } catch (CommentNotFoundException e) { // コメントが見つからなかった,無視 } } return localTopic.calcMessageDataSize(topic, category); }
/** * 指定したトピックのコメントを取得 * * @param topic * @param category * @param fromNo * @return * @throws CommentNotFoundException */ public String getComment(String topic, String category, int fromNo) throws CommentNotFoundException { // メモリ上に存在している場合は,コメントをメモリ上から取得 SortedSet<CommentElement> comments = localTopic.get(topic, category, fromNo); // StringBuffer commentBuf = new StringBuffer("<topic>\n"); StringBuffer commentBuf = new StringBuffer(""); if (comments == null) { // コメントをファイルから持ってくる loadTopicFromFile(topic, category); comments = localTopic.get(topic, category, fromNo); } // コメントを取得 for (CommentElement buf : comments) { commentBuf.append(commentElementToXML(buf) + "\n"); } // 現在表示中のトピックを更新 currentTopic = new Pair<String, String>(topic, category); return commentBuf.toString(); }
/** * メモリ上に保持しているトピックをファイルへ保存する * * @param topic * @param category * @return */ public boolean saveTopicToFile() { Set<Pair<String, String>> topics = localTopic.getTopics(); // データが一つも存在しない if (topics == null) { return false; } // ログディレクトリを作成 try { File logDir = new File("./" + logDirectory); if (!logDir.exists()) { // ディレクトリが存在しないので作成する if (logDir.mkdir() == false) { throw new IOException(logDirectory + "ディレクトリを作成できませんでした."); } } // メモリ上に存在するすべてのトピックをファイルへ保存する for (Pair<String, String> topic : topics) { // ファイル名 ./カテゴリ名/トピック名.dat String filename = "./" + logDirectory + "/" + topic.getSecond() + "/" + topic.getFirst() + ".dat"; // トピック保存ディレクトリを作成 // ディレクトリ名はトピックのカテゴリ名 File dir = new File("./" + logDirectory + "/" + topic.getSecond()); if (!dir.exists()) { // ディレクトリが存在しないので作成する if (dir.mkdir() == false) { throw new IOException("ディレクトリを作成できませんでした."); } } FileOutputStream fos = new FileOutputStream(filename); OutputStreamWriter osw = new OutputStreamWriter(fos, charsetName); BufferedWriter bw = new BufferedWriter(osw); // ファイル書き込みデータ StringBuffer writeData = new StringBuffer("<?xml version=\"1.0\" encoding=\"" + charsetName + "\"?>\n"); writeData.append("<topic>\n"); SortedSet<CommentElement> comments = localTopic.get(topic.getFirst(), topic.getSecond()); // トピックの所属するすべてのコメントデータを書き込む for (CommentElement comment : comments) { writeData.append(commentElementToXML(comment) + "\n"); } writeData.append("</topic>"); // ファイル書き込み bw.write(writeData.toString()); // ファイルを閉じる bw.close(); osw.close(); fos.close(); } } catch (Exception e) { System.err.println(e); e.printStackTrace(); } return false; }