@Override public byte[] serialize(Object obj) throws IOException { try { return marshaller.objectToByteBuffer(obj); } catch (InterruptedException e) { if (log.isTraceEnabled()) log.trace("Interrupted while serializing object"); Thread.currentThread().interrupt(); throw new IOException(e); } }
private Object objectFromInputStreamInReentrantMode(InputStream is) throws IOException, ClassNotFoundException, InterruptedException { int len = is.available(); Object o = null; if (len != 0) { ExposedByteArrayOutputStream bytes = new ExposedByteArrayOutputStream(len); byte[] buf = new byte[Math.min(len, 1024)]; int bytesRead; while ((bytesRead = is.read(buf, 0, buf.length)) != -1) { bytes.write(buf, 0, bytesRead); } is = new ByteArrayInputStream(bytes.getRawBuffer(), 0, bytes.size()); ObjectInput unmarshaller = marshaller.startObjectInput(is, false); try { o = marshaller.objectFromObjectStream(unmarshaller); } finally { marshaller.finishObjectInput(unmarshaller); } } return o; }
@Override public Object deserialize(byte[] buf) throws IOException { try { return marshaller.objectFromByteBuffer(buf); } catch (ClassNotFoundException e) { throw (IOException)new IOException().initCause(e); } }
public void testToStream() throws Exception { cs.store(TestInternalCacheEntryFactory.create("k1", "v1", -1, -1)); StreamingMarshaller marshaller = getMarshaller(); ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutput oo = marshaller.startObjectOutput(out, false, 12); try { cs.toStream(new UnclosableObjectOutputStream(oo)); } finally { marshaller.finishObjectOutput(oo); out.close(); } ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); ObjectInput oi = marshaller.startObjectInput(in, false); try { assert oi.readInt() == 1 : "we have 3 different buckets"; assert oi.readObject().equals(fcs.getLockFromKey("k1") + ""); assert oi.readInt() > 0; //size on disk } finally { marshaller.finishObjectInput(oi); } }
/** * Reads from a stream the number of entries (long) then the entries themselves. */ @Override public void fromStream(ObjectInput in) throws CacheLoaderException { try { log.debug("fromStream()"); int count = 0; while (true) { count++; InternalCacheEntry entry = (InternalCacheEntry) getMarshaller().objectFromObjectStream(in); if (entry == null) break; store(entry); } log.debugf("read %d entries", count); } catch (IOException e) { throw new CacheLoaderException(e); } catch (ClassNotFoundException e) { throw new CacheLoaderException(e); } catch (InterruptedException ie) { if (log.isTraceEnabled()) log.trace("Interrupted while reading from stream"); Thread.currentThread().interrupt(); } }
/** * Writes to a stream the number of entries (long) then the entries themselves. */ @Override public void toStream(ObjectOutput out) throws CacheLoaderException { try { Set<InternalCacheEntry> loadAll = loadAll(); log.debug("toStream() entries"); int count = 0; for (InternalCacheEntry entry : loadAll) { getMarshaller().objectToObjectStream(entry, out); count++; } getMarshaller().objectToObjectStream(null, out); log.debugf("wrote %d entries", count); } catch (IOException e) { throw new CacheLoaderException(e); } }
ObjectOutput oo = marshaller.startObjectOutput(out, false, 12); try { cs.toStream(new UnclosableObjectOutputStream(oo)); } finally { marshaller.finishObjectOutput(oo); out.close(); ObjectInput oi = marshaller.startObjectInput(in, false); try { cs.fromStream(new UnclosableObjectInputStream(oi)); } finally { marshaller.finishObjectInput(oi);
private byte[] marshall(InternalCacheEntry entry) throws IOException, InterruptedException { return getMarshaller().objectToByteBuffer(entry.toInternalCacheValue()); }
private InternalCacheEntry unmarshall(Object o, Object key) throws IOException, ClassNotFoundException { if (o == null) return null; byte b[] = (byte[]) o; InternalCacheValue v = (InternalCacheValue) getMarshaller().objectFromByteBuffer(b); return v.toInternalCacheEntry(key); }
@Override public void objectToEntry(InternalCacheEntry object, DatabaseEntry entry) { byte[] b; try { b = m.objectToByteBuffer(object); } catch (IOException e) { throw new RuntimeExceptionWrapper(e); } catch (InterruptedException ie) { Thread.currentThread().interrupt(); throw new RuntimeExceptionWrapper(ie); } entry.setData(b); } }
@Override public InternalCacheEntry entryToObject(DatabaseEntry entry) { try { return (InternalCacheEntry) m.objectFromByteBuffer(entry.getData()); } catch (IOException e) { throw new RuntimeExceptionWrapper(e); } catch (ClassNotFoundException e) { throw new RuntimeExceptionWrapper(e); } }
@Override public void updateBucket(Bucket b) throws CacheLoaderException { File f = new File(root, b.getBucketIdAsString()); if (f.exists()) { if (!purgeFile(f)) { log.problemsRemovingFile(f); } else if (trace) { log.tracef("Successfully deleted file: '%s'", f.getName()); } } if (!b.getEntries().isEmpty()) { try { byte[] bytes = marshaller.objectToByteBuffer(b); fileSync.write(bytes, f); } catch (IOException ex) { log.errorSavingBucket(b, ex); throw new CacheLoaderException(ex); } catch (InterruptedException ie) { if (trace) { log.trace("Interrupted while marshalling a bucket"); } Thread.currentThread().interrupt(); // Restore interrupted status } } }
if (b == null) continue; InternalCacheValue ice = (InternalCacheValue) getMarshaller().objectFromByteBuffer(b); if (ice.isExpired(currentTimeMillis)) {