/** * Factory method. This method optimises memory by trying to make a better guess than the Java default * of 32 bytes by assuming the starting point for the serialized size will be what it was last time * this method was called. * @param serializable any Object that implements Serializable * @return a ByteArrayOutputStream with a Serialized object in it * @throws java.io.IOException if something goes wrong with the Serialization */ public static MemoryEfficientByteArrayOutputStream serialize(Serializable serializable) throws IOException { MemoryEfficientByteArrayOutputStream outstr = new MemoryEfficientByteArrayOutputStream(lastSize); ObjectOutputStream objstr = new ObjectOutputStream(outstr); objstr.writeObject(serializable); objstr.close(); lastSize = outstr.getBytes().length; return outstr; } }
private MemoryEfficientByteArrayOutputStream serializeElement(Element element) throws IOException { // A ConcurrentModificationException can occur because Java's serialization // mechanism is not threadsafe and POJOs are seldom implemented in a threadsafe way. // e.g. we are serializing an ArrayList field while another thread somewhere in the application is appending to it. try { return MemoryEfficientByteArrayOutputStream.serialize(element); } catch (ConcurrentModificationException e) { throw new CacheException("Failed to serialize element due to ConcurrentModificationException. " + "This is frequently the result of inappropriately sharing thread unsafe object " + "(eg. ArrayList, HashMap, etc) between threads", e); } }
/** * Write the given element to disk, and return the associated marker. * * @param element to write * @return marker representing the element * @throws java.io.IOException on write error */ protected DiskMarker write(Element element) throws IOException { MemoryEfficientByteArrayOutputStream buffer = serializeElement(element); int bufferLength = buffer.size(); elementSize = bufferLength; DiskMarker marker = alloc(element, bufferLength); // Write the record final RandomAccessFile data = getDataAccess(element.getObjectKey()); synchronized (data) { data.seek(marker.getPosition()); data.write(buffer.toByteArray(), 0, bufferLength); } return marker; }
/** * Factory method * @param serializable any Object that implements Serializable * @param estimatedPayloadSize how many bytes is expected to be in the Serialized representation * @return a ByteArrayOutputStream with a Serialized object in it * @throws java.io.IOException if something goes wrong with the Serialization */ public static MemoryEfficientByteArrayOutputStream serialize(Serializable serializable, int estimatedPayloadSize) throws IOException { MemoryEfficientByteArrayOutputStream outstr = new MemoryEfficientByteArrayOutputStream(estimatedPayloadSize); ObjectOutputStream objstr = new ObjectOutputStream(outstr); objstr.writeObject(serializable); objstr.close(); return outstr; }
/** * Factory method * @param serializable any Object that implements Serializable * @param estimatedPayloadSize how many bytes is expected to be in the Serialized representation * @return a ByteArrayOutputStream with a Serialized object in it * @throws java.io.IOException if something goes wrong with the Serialization */ public static MemoryEfficientByteArrayOutputStream serialize(Serializable serializable, int estimatedPayloadSize) throws IOException { MemoryEfficientByteArrayOutputStream outstr = new MemoryEfficientByteArrayOutputStream(estimatedPayloadSize); ObjectOutputStream objstr = new ObjectOutputStream(outstr); objstr.writeObject(serializable); objstr.close(); return outstr; }
/** * Write the given element to disk, and return the associated marker. * * @param element to write * @return marker representing the element * @throws java.io.IOException on write error */ protected DiskMarker write(Element element) throws IOException { MemoryEfficientByteArrayOutputStream buffer = serializeElement(element); int bufferLength = buffer.size(); elementSize = bufferLength; DiskMarker marker = alloc(element, bufferLength); // Write the record final RandomAccessFile data = getDataAccess(element.getObjectKey()); synchronized (data) { data.seek(marker.getPosition()); data.write(buffer.toByteArray(), 0, bufferLength); } return marker; }
/** * Factory method. This method optimises memory by trying to make a better guess than the Java default * of 32 bytes by assuming the starting point for the serialized size will be what it was last time * this method was called. * @param serializable any Object that implements Serializable * @return a ByteArrayOutputStream with a Serialized object in it * @throws java.io.IOException if something goes wrong with the Serialization */ public static MemoryEfficientByteArrayOutputStream serialize(Serializable serializable) throws IOException { MemoryEfficientByteArrayOutputStream outstr = new MemoryEfficientByteArrayOutputStream(lastSize); ObjectOutputStream objstr = new ObjectOutputStream(outstr); objstr.writeObject(serializable); objstr.close(); lastSize = outstr.getBytes().length; return outstr; } }
private MemoryEfficientByteArrayOutputStream serializeElement(Element element) throws IOException { // try two times to Serialize. A ConcurrentModificationException can occur because Java's serialization // mechanism is not threadsafe and POJOs are seldom implemented in a threadsafe way. // e.g. we are serializing an ArrayList field while another thread somewhere in the application is appending to it. // The best we can do is try again and then give up. ConcurrentModificationException exception = null; for (int retryCount = 0; retryCount < 2; retryCount++) { try { return MemoryEfficientByteArrayOutputStream.serialize(element); } catch (ConcurrentModificationException e) { exception = e; try { // wait for the other thread(s) to finish MILLISECONDS.sleep(SERIALIZATION_CONCURRENCY_DELAY); } catch (InterruptedException e1) { //no-op } } } throw exception; }
/** * Factory method * @param serializable any Object that implements Serializable * @param estimatedPayloadSize how many bytes is expected to be in the Serialized representation * @return a ByteArrayOutputStream with a Serialized object in it * @throws java.io.IOException if something goes wrong with the Serialization */ public static MemoryEfficientByteArrayOutputStream serialize(Serializable serializable, int estimatedPayloadSize) throws IOException { MemoryEfficientByteArrayOutputStream outstr = new MemoryEfficientByteArrayOutputStream(estimatedPayloadSize); ObjectOutputStream objstr = new ObjectOutputStream(outstr); objstr.writeObject(serializable); objstr.close(); return outstr; }
/** * Write the given element to disk, and return the associated marker. * * @param element to write * @return marker representing the element * @throws java.io.IOException on write error */ protected DiskMarker write(Element element) throws IOException { MemoryEfficientByteArrayOutputStream buffer = serializeElement(element); int bufferLength = buffer.size(); elementSize = bufferLength; DiskMarker marker = alloc(element, bufferLength); // Write the record final RandomAccessFile data = getDataAccess(element.getObjectKey()); synchronized (data) { data.seek(marker.getPosition()); data.write(buffer.toByteArray(), 0, bufferLength); } return marker; }
/** * Factory method. This method optimises memory by trying to make a better guess than the Java default * of 32 bytes by assuming the starting point for the serialized size will be what it was last time * this method was called. * @param serializable any Object that implements Serializable * @return a ByteArrayOutputStream with a Serialized object in it * @throws java.io.IOException if something goes wrong with the Serialization */ public static MemoryEfficientByteArrayOutputStream serialize(Serializable serializable) throws IOException { MemoryEfficientByteArrayOutputStream outstr = new MemoryEfficientByteArrayOutputStream(lastSize); ObjectOutputStream objstr = new ObjectOutputStream(outstr); objstr.writeObject(serializable); objstr.close(); lastSize = outstr.getBytes().length; return outstr; } }
private MemoryEfficientByteArrayOutputStream serializeElement(Element element) throws IOException { // A ConcurrentModificationException can occur because Java's serialization // mechanism is not threadsafe and POJOs are seldom implemented in a threadsafe way. // e.g. we are serializing an ArrayList field while another thread somewhere in the application is appending to it. try { return MemoryEfficientByteArrayOutputStream.serialize(element); } catch (ConcurrentModificationException e) { throw new CacheException("Failed to serialize element due to ConcurrentModificationException. " + "This is frequently the result of inappropriately sharing thread unsafe object " + "(eg. ArrayList, HashMap, etc) between threads", e); } }
/** * Factory method * @param serializable any Object that implements Serializable * @param estimatedPayloadSize how many bytes is expected to be in the Serialized representation * @return a ByteArrayOutputStream with a Serialized object in it * @throws java.io.IOException if something goes wrong with the Serialization */ public static MemoryEfficientByteArrayOutputStream serialize(Serializable serializable, int estimatedPayloadSize) throws IOException { MemoryEfficientByteArrayOutputStream outstr = new MemoryEfficientByteArrayOutputStream(estimatedPayloadSize); ObjectOutputStream objstr = new ObjectOutputStream(outstr); objstr.writeObject(serializable); objstr.close(); return outstr; }
/** * Write the given element to disk, and return the associated marker. * * @param element to write * @return marker representing the element * @throws java.io.IOException on write error */ protected DiskMarker write(Element element) throws IOException { MemoryEfficientByteArrayOutputStream buffer = serializeElement(element); int bufferLength = buffer.size(); elementSize = bufferLength; DiskMarker marker = alloc(element, bufferLength); // Write the record final RandomAccessFile data = getDataAccess(element.getObjectKey()); synchronized (data) { data.seek(marker.getPosition()); data.write(buffer.toByteArray(), 0, bufferLength); } return marker; }
/** * Factory method. This method optimises memory by trying to make a better guess than the Java default * of 32 bytes by assuming the starting point for the serialized size will be what it was last time * this method was called. * @param serializable any Object that implements Serializable * @return a ByteArrayOutputStream with a Serialized object in it * @throws java.io.IOException if something goes wrong with the Serialization */ public static MemoryEfficientByteArrayOutputStream serialize(Serializable serializable) throws IOException { MemoryEfficientByteArrayOutputStream outstr = new MemoryEfficientByteArrayOutputStream(lastSize); ObjectOutputStream objstr = new ObjectOutputStream(outstr); objstr.writeObject(serializable); objstr.close(); lastSize = outstr.getBytes().length; return outstr; } }
private MemoryEfficientByteArrayOutputStream serializeElement(Element element) throws IOException { // A ConcurrentModificationException can occur because Java's serialization // mechanism is not threadsafe and POJOs are seldom implemented in a threadsafe way. // e.g. we are serializing an ArrayList field while another thread somewhere in the application is appending to it. try { return MemoryEfficientByteArrayOutputStream.serialize(element); } catch (ConcurrentModificationException e) { throw new CacheException("Failed to serialize element due to ConcurrentModificationException. " + "This is frequently the result of inappropriately sharing thread unsafe object " + "(eg. ArrayList, HashMap, etc) between threads", e); } }