示例#1
1
  public E get(long index) {
    RandomAccessFile randomSerializeIndexFile = null;
    RandomAccessFile randomSerializeFile = null;
    E result = null;
    Lock lock = readWriteLock.readLock();
    lock.lock();
    Throwable throwable = null;
    long elementsCount = 0;
    try {
      if (!dataFile.canRead() || !indexFile.canRead()) {
        return null;
      }
      randomSerializeIndexFile = new RandomAccessFile(indexFile, "r");
      randomSerializeFile = new RandomAccessFile(dataFile, "r");
      elementsCount = internalGetSize(randomSerializeIndexFile);
      if (index >= 0 && index < elementsCount) {
        long offset = internalOffsetOfElement(randomSerializeIndexFile, index);
        result = internalReadElement(randomSerializeFile, offset);

        return result;
      }
    } catch (Throwable e) {
      throwable = e;
    } finally {
      IOUtilities.closeQuietly(randomSerializeFile);
      IOUtilities.closeQuietly(randomSerializeIndexFile);
      lock.unlock();
    }

    // it's a really bad idea to log while locked *sigh*
    if (throwable != null) {
      if (throwable instanceof ClassNotFoundException
          || throwable instanceof InvalidClassException) {
        if (logger.isWarnEnabled())
          logger.warn("Couldn't deserialize object at index " + index + "!\n" + throwable);
      } else if (throwable instanceof ClassCastException) {
        if (logger.isWarnEnabled())
          logger.warn("Couldn't cast deserialized object at index " + index + "!\n" + throwable);
      } else {
        if (logger.isWarnEnabled())
          logger.warn("Couldn't retrieve element at index " + index + "!", throwable);
      }
      IOUtilities.interruptIfNecessary(throwable);
    } else if (index < 0 || index >= elementsCount) {
      if (logger.isInfoEnabled()) {
        logger.info(
            "index ("
                + index
                + ") must be in the range [0..<"
                + elementsCount
                + "]. Returning null.");
      }
      return null;
    }

    return result;
  }
示例#2
0
  public void add(E element) {
    RandomAccessFile randomSerializeIndexFile = null;
    RandomAccessFile randomSerializeFile = null;
    Throwable throwable = null;
    Lock lock = readWriteLock.writeLock();
    lock.lock(); // FindBugs "Multithreaded correctness - Method does not release lock on all
    // exception paths" is a false positive
    try {
      randomSerializeIndexFile = new RandomAccessFile(indexFile, "rw");
      randomSerializeFile = new RandomAccessFile(dataFile, "rw");
      long elementsCount = internalGetSize(randomSerializeIndexFile);

      long offset = 0;
      if (elementsCount > 0) {
        long prevElement = elementsCount - 1;
        offset = internalOffsetOfElement(randomSerializeIndexFile, prevElement);
        offset = offset + internalReadElementSize(randomSerializeFile, offset) + 4;
      }
      internalWriteElement(randomSerializeFile, offset, element);
      internalWriteOffset(randomSerializeIndexFile, elementsCount, offset);
    } catch (IOException e) {
      throwable = e;
    } finally {
      IOUtilities.closeQuietly(randomSerializeFile);
      IOUtilities.closeQuietly(randomSerializeIndexFile);
      lock.unlock();
    }
    if (throwable != null) {
      // it's a really bad idea to log while locked *sigh*
      if (logger.isWarnEnabled()) logger.warn("Couldn't write element!", throwable);
      IOUtilities.interruptIfNecessary(throwable);
    }
  }
示例#3
0
  public void addAll(List<E> elements) {
    if (elements != null) {
      int newElementCount = elements.size();
      if (newElementCount > 0) {
        RandomAccessFile randomSerializeIndexFile = null;
        RandomAccessFile randomSerializeFile = null;
        Throwable throwable = null;
        Lock lock = readWriteLock.writeLock();
        lock.lock(); // FindBugs "Multithreaded correctness - Method does not release lock on all
        // exception paths" is a false positive
        try {
          randomSerializeIndexFile = new RandomAccessFile(indexFile, "rw");
          randomSerializeFile = new RandomAccessFile(dataFile, "rw");

          long elementsCount = internalGetSize(randomSerializeIndexFile);

          long offset = 0;
          if (elementsCount > 0) {
            long prevElement = elementsCount - 1;
            offset = internalOffsetOfElement(randomSerializeIndexFile, prevElement);
            offset = offset + internalReadElementSize(randomSerializeFile, offset) + 4;
          }
          long[] offsets = new long[elements.size()];
          int index = 0;
          for (E element : elements) {
            offsets[index] = offset;
            offset = offset + internalWriteElement(randomSerializeFile, offset, element) + 4;
            index++;
          }

          index = 0;
          for (long curOffset : offsets) {
            internalWriteOffset(randomSerializeIndexFile, elementsCount + index, curOffset);
            index++;
          }
          // if(logger.isInfoEnabled()) logger.info("Elements after batch-write: {}",
          // index+elementsCount);
        } catch (Throwable e) {
          throwable = e;
        } finally {
          IOUtilities.closeQuietly(randomSerializeFile);
          IOUtilities.closeQuietly(randomSerializeIndexFile);
          lock.unlock();
        }
        if (throwable != null) {
          // it's a really bad idea to log while locked *sigh*
          if (logger.isWarnEnabled()) logger.warn("Couldn't write element!", throwable);
          IOUtilities.interruptIfNecessary(throwable);
        }
      }
    }
  }
示例#4
0
 public long getSize() {
   RandomAccessFile raf = null;
   Throwable throwable;
   Lock lock = readWriteLock.readLock();
   lock.lock(); // FindBugs "Multithreaded correctness - Method does not release lock on all
   // exception paths" is a false positive
   try {
     if (!indexFile.canRead()) {
       return 0;
     }
     raf = new RandomAccessFile(indexFile, "r");
     return internalGetSize(raf);
   } catch (Throwable e) {
     throwable = e;
   } finally {
     IOUtilities.closeQuietly(raf);
     lock.unlock();
   }
   // it's a really bad idea to log while locked *sigh*
   if (throwable != null) {
     if (logger.isDebugEnabled()) logger.debug("Couldn't retrieve size!", throwable);
     IOUtilities.interruptIfNecessary(throwable);
   }
   return 0;
 }
示例#5
0
 private void writeImage(File imageFile) throws IOException {
   BufferedImage resultImage = null;
   if (selectedGraph >= 0 && selectedGraph < graphImageFactories.length) {
     resultImage =
         graphImageFactories[selectedGraph].createGraphImage(
             Util.getTime(), sourceIdentifier, null, showMaxCheckBox.isSelected());
   }
   if (resultImage != null) {
     final String format = "png";
     BufferedOutputStream imageOutput = null;
     try {
       imageOutput = new BufferedOutputStream(new FileOutputStream(imageFile));
       boolean writerFound = ImageIO.write(resultImage, format, imageOutput);
       if (!writerFound) {
         String msg = "Couldn't write image! No writer found for format '" + format + "'!";
         if (logger.isErrorEnabled()) logger.error(msg);
         throw new IOException(msg);
       }
     } finally {
       // close output stream no matter what. shouldn't be necessary...
       IOUtilities.closeQuietly(imageOutput);
       resultImage.flush();
     }
   }
 }
 public void close() {
   IOUtilities.closeQuietly(inputStream);
 }