Class MemoryResource

java.lang.Object
io.smallrye.common.resource.Resource
io.smallrye.common.resource.MemoryResource

public final class MemoryResource extends Resource
An in-memory resource.
  • Constructor Details

    • MemoryResource

      public MemoryResource(String pathName, ByteBuffer data)
      Construct a new instance. The given buffer contents are not copied. Accessing the resource will not affect the buffer's position or limit. Modifying the buffer's position or limit will not affect the contents of the resource.
      Parameters:
      pathName - the resource path name (must not be null)
      data - the byte buffer containing the resource data (must not be null)
    • MemoryResource

      public MemoryResource(String pathName, byte[] data)
      Construct a new instance for a byte array. The byte array is not copied.
      Parameters:
      pathName - the resource path name (must not be null)
      data - the byte array (must not be null)
  • Method Details

    • url

      public URL url()
      Description copied from class: Resource
      Returns the resource URL (not null). If the resource location information cannot be converted to a URL, an exception may be thrown.
      Specified by:
      url in class Resource
      Returns:
      the resource URL (not null)
    • openStream

      public MemoryInputStream openStream()
      Description copied from class: Resource
      Open an input stream to read this resource.
      Specified by:
      openStream in class Resource
      Returns:
      the input stream (not null)
    • asBuffer

      public ByteBuffer asBuffer()
      Description copied from class: Resource
      Returns the bytes of this resource, as a read-only byte buffer. The buffer is suitable for passing to ClassLoader.defineClass(String, ByteBuffer, ProtectionDomain). The default implementation reads all of the resource bytes from the stream returned by Resource.openStream(). Other implementations might return a buffer for data already contained in memory, or might return a memory-mapped buffer if the resource is very large. The buffer might or might not be cached on the resource. Because of this, care should be taken to avoid calling this method repeatedly for a single resource.
      Overrides:
      asBuffer in class Resource
      Returns:
      the bytes of this resource, as a read-only byte buffer
    • copyTo

      public long copyTo(Path destination) throws IOException
      Description copied from class: Resource
      Copy the bytes of this resource to the given destination. The copy may fail before all of the bytes have been transferred; in this case the content and state of the destination are undefined.

      The path is opened as if with the following options:

      Overrides:
      copyTo in class Resource
      Parameters:
      destination - the destination path (must not be null)
      Returns:
      the number of bytes copied
      Throws:
      IOException - if the copy fails
    • copyTo

      public long copyTo(WritableByteChannel channel) throws IOException
      Description copied from class: Resource
      Copy the bytes of this resource to the given destination. The copy may fail before all of the bytes have been transferred; in this case the content and state of the destination are undefined. The destination channel is not closed.
      Overrides:
      copyTo in class Resource
      Parameters:
      channel - the destination channel (must not be null and must not be non-blocking)
      Returns:
      the number of bytes copied
      Throws:
      IOException - if the copy fails
    • copyTo

      public long copyTo(OutputStream destination) throws IOException
      Description copied from class: Resource
      Copy the bytes of this resource to the given destination. The copy may fail before all of the bytes have been transferred; in this case the content and state of the destination are undefined. The destination stream is not closed.
      Overrides:
      copyTo in class Resource
      Parameters:
      destination - the destination stream (must not be null)
      Returns:
      the number of bytes copied
      Throws:
      IOException - if the copy fails
    • modifiedTime

      public Instant modifiedTime()
      Description copied from class: Resource
      Returns the modification time of the resource, or null if the time is unknown.
      Overrides:
      modifiedTime in class Resource
      Returns:
      the modification time of the resource, or null if the time is unknown
    • size

      public long size()
      Description copied from class: Resource
      Returns the size of the resource, or -1 if the size is not known.
      Specified by:
      size in class Resource
      Returns:
      the size of the resource, or -1 if the size is not known