package org.apache.lucene.util;

/**
 * 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.
 */

import java.util.Arrays;
import java.util.Random;

import org.junit.Ignore;

@Ignore("You must increase heap to > 2 G to run this")
// nocommit: write this test in some other way (not indexinput/output)
public class Test2BPagedBytes extends LuceneTestCase {
/*
  public void test() throws Exception {
    PagedBytes pb = new PagedBytes(15);
    PagedBytesDataOutput dataOutput = pb.getDataOutput();
    long netBytes = 0;
    long seed = random().nextLong();
    long lastFP = 0;
    Random r2 = new Random(seed);
    while(netBytes < 1.1*Integer.MAX_VALUE) {
      int numBytes = _TestUtil.nextInt(r2, 1, 100000);
      byte[] bytes = new byte[numBytes];
      r2.nextBytes(bytes);
      dataOutput.writeBytes(bytes, bytes.length);
      long fp = dataOutput.getPosition();
      assert fp == lastFP + numBytes;
      lastFP = fp;
      netBytes += numBytes;
    }
    pb.freeze(true);

    PagedBytesDataInput dataInput = pb.getDataInput();
    lastFP = 0;
    r2 = new Random(seed);
    netBytes = 0;
    while(netBytes < 1.1*Integer.MAX_VALUE) {
      int numBytes = _TestUtil.nextInt(r2, 1, 100000);
      byte[] bytes = new byte[numBytes];
      r2.nextBytes(bytes);

      byte[] bytesIn = new byte[numBytes];
      dataInput.readBytes(bytesIn, 0, numBytes);
      assertTrue(Arrays.equals(bytes, bytesIn));

      long fp = dataInput.getPosition();
      assert fp == lastFP + numBytes;
      lastFP = fp;
      netBytes += numBytes;
    }
  } */
}

@@ -851,41 +851,251 @@
 til.
-Arrays;%0Aimport java.util.Random;%0A
+Random;%0A%0Aimport org.apache.lucene.store.BaseDirectoryWrapper;%0Aimport org.apache.lucene.store.IOContext;%0Aimport org.apache.lucene.store.IndexInput;%0Aimport org.apache.lucene.store.IndexOutput;%0Aimport org.apache.lucene.store.MockDirectoryWrapper;
 %0Aimp
@@ -1176,79 +1176,8 @@
 s%22)%0A
-// nocommit: write this test in some other way (not indexinput/output)%0A
 publ
@@ -1227,18 +1227,16 @@
 tCase %7B%0A
-/*
 %0A  publi
@@ -1268,16 +1268,246 @@
 ption %7B%0A
+    BaseDirectoryWrapper dir = newFSDirectory(_TestUtil.getTempDir(%22test2BPagedBytes%22));%0A    if (dir instanceof MockDirectoryWrapper) %7B%0A      ((MockDirectoryWrapper)dir).setThrottling(MockDirectoryWrapper.Throttling.NEVER);%0A    %7D%0A
     Page
@@ -1542,30 +1542,21 @@
 5);%0A    
-PagedBytesData
+Index
 Output d
@@ -1571,25 +1571,49 @@
 t = 
-pb.getData
+dir.create
 Output(
+%22foo%22, IOContext.DEFAULT
 );%0A 
@@ -1810,38 +1810,37 @@
 .nextInt(r2, 1, 
-100000
+32768
 );%0A      byte%5B%5D 
@@ -1970,32 +1970,35 @@
 taOutput.get
-Position
+FilePointer
 ();%0A      as
@@ -2088,96 +2088,195 @@
     
-pb.freeze(true
+dataOutput.close(
 );%0A
-%0A
     
-PagedBytesDataInput dataInput = pb.getDataInput();%0A    lastFP = 0;
+IndexInput input = dir.openInput(%22foo%22, IOContext.DEFAULT);%0A    pb.copy(input, input.length());%0A    input.close();%0A    PagedBytes.Reader reader = pb.freeze(true);%0A
 %0A   
@@ -2413,14 +2413,13 @@
  1, 
-100000
+32768
 );%0A 
@@ -2489,98 +2489,93 @@
 s);%0A
-%0A
       
-byte%5B%5D bytesIn
+BytesRef expected
  = new 
-byte%5BnumB
+BytesRef(b
 ytes
-%5D;
+);%0A
 %0A      
-dataInput.readBytes(bytesIn, 0, num
+BytesRef actual = new 
 Bytes
+Ref(
 );%0A 
@@ -2583,149 +2583,92 @@
     
-assertTrue(Arrays.equals(bytes, bytesIn));%0A%0A      long fp = dataInput.getPosition();%0A      assert fp == lastFP + numBytes;%0A      lastFP = fp;
+reader.fillSlice(actual, netBytes, numBytes);%0A      assertEquals(expected, actual);%0A
 %0A   
@@ -2704,11 +2704,25 @@
 %7D%0A  
-%7D */
+  dir.close();%0A  %7D
 %0A%7D%0A

fix nocommit