/**
  * Returns the length in bytes of a file in the directory.
  *
  * @throws IOException if the file does not exist
  */
 @Override
 public final long fileLength(String name) throws IOException {
   ensureOpen();
   RAMFile file = fileMap.get(name);
   if (file == null) {
     throw new FileNotFoundException(name);
   }
   return file.getLength();
 }
 private final void switchCurrentBuffer() throws IOException {
   if (currentBufferIndex == file.numBuffers()) {
     currentBuffer = file.addBuffer(BUFFER_SIZE);
   } else {
     currentBuffer = file.getBuffer(currentBufferIndex);
   }
   bufferPosition = 0;
   bufferStart = (long) BUFFER_SIZE * (long) currentBufferIndex;
   bufferLength = currentBuffer.length;
 }
 @Override
 public void deleteFile(String name) throws IOException {
   ensureOpen();
   RAMFile file = fileMap.remove(name);
   if (file != null) {
     file.directory = null;
     sizeInBytes.addAndGet(-file.sizeInBytes);
   } else {
     throw new FileNotFoundException(name);
   }
 }
 @Override
 public IndexOutput createOutput(String name, IOContext context) throws IOException {
   ensureOpen();
   RAMFile file = newRAMFile();
   RAMFile existing = fileMap.remove(name);
   if (existing != null) {
     sizeInBytes.addAndGet(-existing.sizeInBytes);
     existing.directory = null;
   }
   fileMap.put(name, file);
   return new RAMOutputStream(name, file, true);
 }
 /** Resets this to an empty file. */
 public void reset() {
   currentBuffer = null;
   currentBufferIndex = -1;
   bufferPosition = 0;
   bufferStart = 0;
   bufferLength = 0;
   file.setLength(0);
 }
 private final void switchCurrentBuffer(boolean enforceEOF) throws IOException {
   bufferStart = (long) BUFFER_SIZE * (long) currentBufferIndex;
   if (currentBufferIndex >= file.numBuffers()) {
     // end of file reached, no more buffers left
     if (enforceEOF) {
       throw new EOFException("Read past EOF (resource: " + this + ")");
     } else {
       // Force EOF if a read takes place at this position
       currentBufferIndex--;
       bufferPosition = BUFFER_SIZE;
     }
   } else {
     currentBuffer = file.getBuffer(currentBufferIndex);
     bufferPosition = 0;
     long buflen = length - bufferStart;
     bufferLength = buflen > BUFFER_SIZE ? BUFFER_SIZE : (int) buflen;
   }
 }
 /** Copy the current contents of this buffer to the named output. */
 public void writeTo(IndexOutput out) throws IOException {
   flush();
   final long end = file.length;
   long pos = 0;
   int buffer = 0;
   while (pos < end) {
     int length = BUFFER_SIZE;
     long nextPos = pos + length;
     if (nextPos > end) { // at the last buffer
       length = (int) (end - pos);
     }
     out.writeBytes(file.getBuffer(buffer++), length);
     pos = nextPos;
   }
 }
 /** Copy the current contents of this buffer to output byte array */
 public void writeTo(byte[] bytes, int offset) throws IOException {
   flush();
   final long end = file.length;
   long pos = 0;
   int buffer = 0;
   int bytesUpto = offset;
   while (pos < end) {
     int length = BUFFER_SIZE;
     long nextPos = pos + length;
     if (nextPos > end) { // at the last buffer
       length = (int) (end - pos);
     }
     System.arraycopy(file.getBuffer(buffer++), 0, bytes, bytesUpto, length);
     bytesUpto += length;
     pos = nextPos;
   }
 }
예제 #9
0
 /** @return size in bytes */
 public long getSizeInBytes() {
   return buffer.getSizeInBytes();
 }
 /** Returns byte usage of all buffers. */
 public long sizeInBytes() {
   return file.numBuffers() * BUFFER_SIZE;
 }
 private void setFileLength() {
   long pointer = bufferStart + bufferPosition;
   if (pointer > file.length) {
     file.setLength(pointer);
   }
 }