jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mreut...@apache.org
Subject svn commit: r713072 - /jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/journal/FileRecordLog.java
Date Tue, 11 Nov 2008 16:04:53 GMT
Author: mreutegg
Date: Tue Nov 11 08:04:52 2008
New Revision: 713072

URL: http://svn.apache.org/viewvc?rev=713072&view=rev
Log:
JCR-1850: Journal: Use buffered input / output streams

Modified:
    jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/journal/FileRecordLog.java

Modified: jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/journal/FileRecordLog.java
URL: http://svn.apache.org/viewvc/jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/journal/FileRecordLog.java?rev=713072&r1=713071&r2=713072&view=diff
==============================================================================
--- jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/journal/FileRecordLog.java
(original)
+++ jackrabbit/trunk/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/journal/FileRecordLog.java
Tue Nov 11 08:04:52 2008
@@ -31,6 +31,8 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayOutputStream;
 
 /**
  * A file record log is a file containing {@link Record}s. Every file record
@@ -140,7 +142,8 @@
         this.logFile = logFile;
 
         if (logFile.exists()) {
-            DataInputStream in = new DataInputStream(new FileInputStream(logFile));
+            DataInputStream in = new DataInputStream(
+                    new BufferedInputStream(new FileInputStream(logFile), 128));
 
             try {
                 readHeader(in);
@@ -160,7 +163,8 @@
      */
     public void init(long previousRevision) throws IOException {
         if (isNew) {
-            DataOutputStream out = new DataOutputStream(new FileOutputStream(logFile));
+            DataOutputStream out = new DataOutputStream(
+                    new BufferedOutputStream(new FileOutputStream(logFile), 128));
 
             try {
                 writeHeader(out);
@@ -274,12 +278,14 @@
     public long append(String journalId, String producerId, InputStream in, int length)
             throws IOException {
 
-        DataOutputStream out = new DataOutputStream(new FileOutputStream(logFile, true));
+        OutputStream out = new FileOutputStream(logFile, true);
 
         try {
-            out.writeUTF(journalId);
-            out.writeUTF(producerId);
-            out.writeInt(length);
+            DataBuffer buffer = new DataBuffer();
+            buffer.writeUTF(journalId);
+            buffer.writeUTF(producerId);
+            buffer.writeInt(length);
+            buffer.copy(out);
 
             IOUtils.copy(in, out);
             out.flush();
@@ -426,4 +432,28 @@
         }
         return utflen;
     }
+
+    /**
+     * A simple helper class that writes to a buffer. The current buffer can
+     * be {@link #copy copied} to an output stream.
+     */
+    private static final class DataBuffer extends DataOutputStream {
+
+        public DataBuffer() {
+            super(new ByteArrayOutputStream());
+        }
+
+        /**
+         * Copies the bytes the are currently held in the buffer to the given
+         * output stream.
+         *
+         * @param out the output stream where the buffered data is written.
+         * @throws IOException if an error occurs while writing data to
+         *          <code>out</code>.
+         */
+        public void copy(OutputStream out) throws IOException {
+            byte[] buffer = ((ByteArrayOutputStream) super.out).toByteArray();
+            out.write(buffer);
+        }
+    }
 }



Mime
View raw message