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; }
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; }
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); } }
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); } } } }
public void run() { try { ArrayList<Byte> bytes = new ArrayList<>(); for (; ; ) { for (; ; ) { int readByte = inputStream.read(); if (readByte == -1) { if (logger.isInfoEnabled()) logger.info("Read -1!!"); return; } byte current = (byte) readByte; if (current == 0) { break; } bytes.add(current); } if (bytes.size() > 0) { byte[] ba = new byte[bytes.size()]; for (int i = 0; i < bytes.size(); i++) { ba[i] = bytes.get(i); } bytes.clear(); String str = new String(ba, StandardCharsets.UTF_8); if (logger.isDebugEnabled()) logger.debug("Read: {}", str); StringReader strr = new StringReader(str); XMLStreamReader reader = XML_INPUT_FACTORY.createXMLStreamReader(strr); LoggingEvent event = loggingEventReader.read(reader); addEvent(event); } else { if (logger.isDebugEnabled()) logger.debug("bytes.size()==0!!"); } } } catch (Throwable e) { if (logger.isDebugEnabled()) logger.debug( "Exception ({}: '{}') while reading events. Adding eventWrapper with empty event and stopping...", e.getClass().getName(), e.getMessage(), e); addEvent(null); IOUtilities.interruptIfNecessary(e); } finally { close(); } }
private void fireTableChanged(TableModelEvent event) { Object[] listeners; synchronized (eventListenerList) { listeners = eventListenerList.getListenerList(); } // Process the listeners last to first, notifying // those that are interested in this event for (int i = listeners.length - 2; i >= 0; i -= 2) { if (listeners[i] == TableModelListener.class) { TableModelListener listener = ((TableModelListener) listeners[i + 1]); if (logger.isDebugEnabled()) { logger.debug("Firing TableChange at {}.", listener.getClass().getName()); } try { listener.tableChanged(event); } catch (Throwable ex) { if (logger.isWarnEnabled()) logger.warn("Exception while firing change!", ex); IOUtilities.interruptIfNecessary(ex); } } } }