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); } }
/** * Tests the entry encoding and decoding process the version 1 encoding. * * @throws Exception If the test failed unexpectedly. */ @Test public void testEntryToAndFromDatabaseV1() throws Exception { ensureServerIsUpAndRunning(); // Convert the test LDIF string to a byte array byte[] originalLDIFBytes = StaticUtils.getBytes(ldifString); try (final LDIFReader reader = new LDIFReader(new LDIFImportConfig(new ByteArrayInputStream(originalLDIFBytes)))) { Entry entryBefore, entryAfterV1; while ((entryBefore = reader.readEntry(false)) != null) { ByteStringBuilder bsb = new ByteStringBuilder(); encodeV1(entryBefore, bsb); entryAfterV1 = Entry.decode(bsb.asReader()); assertEquals(entryBefore, entryAfterV1); } } }
/** * Tests the entry encoding and decoding process the version 1 encoding. * * @throws Exception If the test failed unexpectedly. */ @Test() public void testEntryToAndFromDatabaseV1() throws Exception { // Make sure that the server is up and running. TestCaseUtils.startServer(); // Convert the test LDIF string to a byte array byte[] originalLDIFBytes = StaticUtils.getBytes(ldifString); LDIFReader reader = new LDIFReader(new LDIFImportConfig(new ByteArrayInputStream(originalLDIFBytes))); Entry entryBefore, entryAfterV1; while ((entryBefore = reader.readEntry(false)) != null) { ByteStringBuilder bsb = new ByteStringBuilder(); encodeV1(entryBefore, bsb); entryAfterV1 = Entry.decode(bsb.asReader()); assertEquals(entryBefore, entryAfterV1); } reader.close(); }
/** * Tests the entry encoding and decoding process the version 1 encoding. * * @throws Exception If the test failed unexpectedly. */ @Test(dataProvider = "encodeConfigs") public void testEntryToAndFromDatabaseV3(EntryEncodeConfig config) throws Exception { ensureServerIsUpAndRunning(); // Convert the test LDIF string to a byte array byte[] originalLDIFBytes = StaticUtils.getBytes(ldifString); try (final LDIFReader reader = new LDIFReader(new LDIFImportConfig(new ByteArrayInputStream(originalLDIFBytes)))) { Entry entryBefore, entryAfterV3; while ((entryBefore = reader.readEntry(false)) != null) { ByteStringBuilder bsb = new ByteStringBuilder(); entryBefore.encode(bsb, config); entryAfterV3 = Entry.decode(bsb.asReader()); if (config.excludeDN()) { entryAfterV3.setDN(entryBefore.getName()); } assertEquals(entryBefore, entryAfterV3); } } }
/** * Tests the entry encoding and decoding process the version 1 encoding. * * @throws Exception If the test failed unexpectedly. */ @Test(dataProvider = "encodeConfigs") public void testEntryToAndFromDatabaseV2(EntryEncodeConfig config) throws Exception { // Make sure that the server is up and running. TestCaseUtils.startServer(); // Convert the test LDIF string to a byte array byte[] originalLDIFBytes = StaticUtils.getBytes(ldifString); LDIFReader reader = new LDIFReader(new LDIFImportConfig(new ByteArrayInputStream(originalLDIFBytes))); Entry entryBefore, entryAfterV2; while ((entryBefore = reader.readEntry(false)) != null) { ByteStringBuilder bsb = new ByteStringBuilder(); encodeV2(entryBefore, bsb, config); entryAfterV2 = Entry.decode(bsb.asReader()); if (config.excludeDN()) { entryAfterV2.setDN(entryBefore.getDN()); } assertEquals(entryBefore, entryAfterV2); } reader.close(); }