private void encodeVolatile(Entry entry, DataConfig dataConfig) throws DirectoryException { // Encode the entry for later use. entry.encode(entryBuffer, dataConfig.getEntryEncodeConfig()); // First write the DB format version byte. encodedBuffer.appendByte(DnKeyFormat.FORMAT_VERSION); try { // Then start the ASN1 sequence. writer.writeStartSequence(TAG_TREE_ENTRY); if (dataConfig.isCompressed()) { OutputStream compressor = null; try { compressor = new DeflaterOutputStream(compressedEntryBuffer.asOutputStream()); entryBuffer.copyTo(compressor); } finally { closeSilently(compressor); } // Compression needed and successful. writer.writeInteger(entryBuffer.length()); writer.writeOctetString(compressedEntryBuffer); } else { writer.writeInteger(0); writer.writeOctetString(entryBuffer); } writer.writeEndSequence(); } catch (IOException ioe) { // TODO: This should never happen with byte buffer. logger.traceException(ioe); } }
private Entry decode(ByteString bytes, CompressedSchema compressedSchema) throws DirectoryException, DecodeException, IOException { // Get the format version. byte formatVersion = bytes.byteAt(0); if (formatVersion != DnKeyFormat.FORMAT_VERSION) { throw DecodeException.error(ERR_INCOMPATIBLE_ENTRY_VERSION.get(formatVersion)); } // Read the ASN1 sequence. ASN1Reader reader = ASN1.getReader(bytes.subSequence(1, bytes.length())); reader.readStartSequence(); // See if it was compressed. int uncompressedSize = (int) reader.readInteger(); if (uncompressedSize > 0) { // It was compressed. reader.readOctetString(compressedEntryBuffer); OutputStream decompressor = null; try { // TODO: Should handle the case where uncompress fails decompressor = new InflaterOutputStream(entryBuffer.asOutputStream()); compressedEntryBuffer.copyTo(decompressor); } finally { closeSilently(decompressor); } // Since we are used the cached buffers (ByteStringBuilders), // the decoded attribute values will not refer back to the // original buffer. return Entry.decode(entryBuffer.asReader(), compressedSchema); } else { // Since we don't have to do any decompression, we can just decode // the entry directly. ByteString encodedEntry = reader.readOctetString(); return Entry.decode(encodedEntry.asReader(), compressedSchema); } }
private void release() { closeSilently(writer); encodedBuffer.clearAndTruncate(maxBufferSize, BUFFER_INIT_SIZE); entryBuffer.clearAndTruncate(maxBufferSize, BUFFER_INIT_SIZE); compressedEntryBuffer.clearAndTruncate(maxBufferSize, BUFFER_INIT_SIZE); }