Index: oak-segment/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentStream.java =================================================================== --- oak-segment/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentStream.java (revision 1732037) +++ oak-segment/src/main/java/org/apache/jackrabbit/oak/plugins/segment/SegmentStream.java (working copy) @@ -200,7 +200,12 @@ if (inline != null) { return (int) (length - position); // <= inline.length } else { - return 0; + long delta = length - position; + if (delta > Integer.MAX_VALUE) { + return 0; + } else { + return (int) delta; + } } } Index: oak-segment/src/test/java/org/apache/jackrabbit/oak/plugins/segment/SegmentStreamTest.java =================================================================== --- oak-segment/src/test/java/org/apache/jackrabbit/oak/plugins/segment/SegmentStreamTest.java (revision 0) +++ oak-segment/src/test/java/org/apache/jackrabbit/oak/plugins/segment/SegmentStreamTest.java (revision 0) @@ -0,0 +1,78 @@ +/* + * 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.oak.plugins.segment; + +import static org.junit.Assert.assertEquals; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Random; + +import org.apache.jackrabbit.oak.api.Blob; +import org.apache.jackrabbit.oak.api.Type; +import org.apache.jackrabbit.oak.spi.commit.CommitInfo; +import org.apache.jackrabbit.oak.spi.commit.EmptyHook; +import org.apache.jackrabbit.oak.spi.state.NodeBuilder; +import org.apache.jackrabbit.oak.spi.state.NodeStore; +import org.junit.Test; + +/** + * Test case for ensuring that segment size remains within bounds. + */ +public class SegmentStreamTest { + + @Test + public void testStream() throws Exception { + SegmentNodeStore nodeStore = new SegmentNodeStore(); + NodeBuilder nb = nodeStore.getRoot().builder(); + NodeBuilder cb = nb.child("test"); + + int smallSize = 10 * 1024; + cb.setProperty("blob1", createBlob(nodeStore, smallSize)); + + int largeSize = Segment.MEDIUM_LIMIT + 50; + cb.setProperty("blob2", createBlob(nodeStore, largeSize)); + nodeStore.merge(nb, EmptyHook.INSTANCE, CommitInfo.EMPTY); + + InputStream small = nodeStore.getRoot().getChildNode("test") + .getProperty("blob1").getValue(Type.BINARY).getNewStream(); + testStream(small, smallSize); + + InputStream large = nodeStore.getRoot().getChildNode("test") + .getProperty("blob2").getValue(Type.BINARY).getNewStream(); + testStream(large, largeSize); + } + + private void testStream(InputStream is, int size) throws Exception { + assertEquals(is.available(), size); + + int toread = size / 2; + byte[] target = new byte[toread]; + int read = is.read(target); + assertEquals(toread, read); + assertEquals(is.available(), size - toread); + } + + private static Blob createBlob(NodeStore nodeStore, int size) + throws IOException { + byte[] data = new byte[size]; + new Random().nextBytes(data); + return nodeStore.createBlob(new ByteArrayInputStream(data)); + } + +}