Return-Path: X-Original-To: apmail-jackrabbit-commits-archive@www.apache.org Delivered-To: apmail-jackrabbit-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9DD8645C5 for ; Wed, 8 Jun 2011 09:19:43 +0000 (UTC) Received: (qmail 18105 invoked by uid 500); 8 Jun 2011 09:19:43 -0000 Delivered-To: apmail-jackrabbit-commits-archive@jackrabbit.apache.org Received: (qmail 18039 invoked by uid 500); 8 Jun 2011 09:19:43 -0000 Mailing-List: contact commits-help@jackrabbit.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@jackrabbit.apache.org Delivered-To: mailing list commits@jackrabbit.apache.org Received: (qmail 18032 invoked by uid 99); 8 Jun 2011 09:19:43 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 08 Jun 2011 09:19:43 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 08 Jun 2011 09:19:42 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id F2CB1238890A; Wed, 8 Jun 2011 09:19:21 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1133296 - /jackrabbit/sandbox/microkernel/src/main/java/org/apache/jackrabbit/mk/util/MicroKernelInputStream.java Date: Wed, 08 Jun 2011 09:19:21 -0000 To: commits@jackrabbit.apache.org From: thomasm@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110608091921.F2CB1238890A@eris.apache.org> 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; + } + +}