/*
 * Copyright (c) 1998-2019 University Corporation for Atmospheric Research/Unidata
 * See LICENSE for license information.
 */
package ucar.unidata.io;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import ucar.nc2.util.IO;
import ucar.unidata.io.spi.RandomAccessFileProvider;

/**
 * A RandomAccessFile stored entirely in memory as a byte array.
 * 
 * @author john
 */
public class InMemoryRandomAccessFile extends ucar.unidata.io.RandomAccessFile {

  /**
   * A RandomAccessFile stored entirely in memory as a byte array.
   *
   * @param name used as the location
   * @param data the complete data file
   */
  public InMemoryRandomAccessFile(String name, byte[] data) {
    super(1);
    this.location = name;
    this.file = null;
    if (data == null)
      throw new IllegalArgumentException("data array is null");

    buffer = data;
    bufferStart = 0;
    dataSize = buffer.length;
    dataEnd = buffer.length;
    filePosition = 0;
    endOfFile = false;

    if (debugLeaks)
      openFiles.add(location);
  }

  @Override
  public long length() {
    return dataEnd;
  }

  // @Override LOOK weird error
  public void setBufferSize(int bufferSize) {
    // do nothing
  }

  @Override
  protected int read_(long pos, byte[] b, int offset, int len) {
    len = Math.min(len, (int) (buffer.length - pos));
    // copy out of buffer
    System.arraycopy(buffer, (int) pos, b, offset, len);
    return len;
  }

  @Override
  public long readToByteChannel(WritableByteChannel dest, long offset, long nbytes) throws IOException {
    return dest.write(ByteBuffer.wrap(buffer, (int) offset, (int) nbytes));
  }

  /**
   * Hook for service provider interface RandomAccessFileProvider
   */
  public static class Provider implements RandomAccessFileProvider {

    @Override
    public boolean isOwnerOf(String location) {
      return location.startsWith("slurp:");
    }

    @Override
    public RandomAccessFile open(String location) throws IOException {
      String scheme = location.split(":")[0];
      location = location.replace(scheme, "http");
      byte[] contents = IO.readURLContentsToByteArray(location); // read all into memory
      return new InMemoryRandomAccessFile(location, contents);
    }
  }

}

@@ -262,24 +262,141 @@
 ort 
-ucar.nc2.util.IO
+java.util.Optional;%0Aimport ucar.httpservices.HTTPException;%0Aimport ucar.httpservices.HTTPFactory;%0Aimport ucar.httpservices.HTTPMethod
 ;%0Aim
@@ -2241,22 +2241,32 @@
 ;%0A      
+Optional%3C
 byte%5B%5D
+%3E
  content
@@ -2270,75 +2270,234 @@
 ents
- = IO.readURLContentsToByteArray(location); // read all into memory
+;%0A      try (HTTPMethod method = HTTPFactory.Get(location)) %7B%0A        method.execute();%0A        contents = Optional.of(method.getResponseAsBytes());%0A      %7D catch (HTTPException he) %7B%0A        throw new IOException(he);%0A      %7D
 %0A   
@@ -2553,16 +2553,22 @@
 contents
+.get()
 );%0A    %7D

Update InMemoryRandomAccessFile to use httpservices