You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jackrabbit.apache.org by th...@apache.org on 2011/06/08 11:19:21 UTC

svn commit: r1133296 - /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/MicroKernelInputStream.java

Author: thomasm
Date: Wed Jun  8 09:19:21 2011
New Revision: 1133296

URL: http://svn.apache.org/viewvc?rev=1133296&view=rev
Log:
Data store helper class.

Added:
    jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/MicroKernelInputStream.java

Added: jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/MicroKernelInputStream.java
URL: http://svn.apache.org/viewvc/jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/MicroKernelInputStream.java?rev=1133296&view=auto
==============================================================================
--- jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/MicroKernelInputStream.java (added)
+++ jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/MicroKernelInputStream.java Wed Jun  8 09:19:21 2011
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.jackrabbit.mk.util;
+
+import java.io.IOException;
+import java.io.InputStream;
+import org.apache.jackrabbit.mk.api.MicroKernel;
+
+/**
+ * An input stream to simplify reading a blob from the micro kernel.
+ */
+public class MicroKernelInputStream extends InputStream {
+
+    private final MicroKernel mk;
+    private final String id;
+    private long pos;
+    private byte[] oneByteBuff;
+
+    MicroKernelInputStream(MicroKernel mk, String id) {
+        this.mk = mk;
+        this.id = id;
+    }
+
+    public int read(byte[] b, int off, int len) {
+        int l = mk.read(id, pos, b, off, len);
+        if (len < 0) {
+            return l;
+        }
+        pos += l;
+        return l;
+    }
+
+    public int read() throws IOException {
+        if (oneByteBuff == null) {
+            oneByteBuff = new byte[1];
+        }
+        int len = mk.read(id, pos, oneByteBuff, 0, 1);
+        if (len < 0) {
+            return len;
+        }
+        pos++;
+        return oneByteBuff[0] & 0xff;
+    }
+
+    public static byte[] readFully(MicroKernel mk, String id) {
+        int len = (int) mk.getLength(id);
+        byte[] buff = new byte[len];
+        MicroKernelInputStream in = new MicroKernelInputStream(mk, id);
+        int off = 0, remaining = len;
+        while (remaining > 0) {
+            int l = in.read(buff, off, remaining);
+            off += l;
+            remaining -= l;
+        }
+        return buff;
+    }
+
+}