You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2018/11/17 18:43:41 UTC

[directory-ldap-api] 01/03: Added the Asn1Buffer class. It's not being used atm.

This is an automated email from the ASF dual-hosted git repository.

elecharny pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/directory-ldap-api.git

commit 4f0e88d99911be9bf6abe51c2ddb6aab1bab1812
Author: elecharny <el...@pierre-arnauds-macbook-pro.home>
AuthorDate: Thu Nov 15 17:59:28 2018 +0100

    Added the Asn1Buffer class. It's not being used atm.
---
 .../apache/directory/api/asn1/util/Asn1Buffer.java | 153 +++++++++++++++++++++
 .../directory/api/asn1/util/Asn1BufferTest.java    | 115 ++++++++++++++++
 2 files changed, 268 insertions(+)

diff --git a/asn1/api/src/main/java/org/apache/directory/api/asn1/util/Asn1Buffer.java b/asn1/api/src/main/java/org/apache/directory/api/asn1/util/Asn1Buffer.java
new file mode 100644
index 0000000..833424e
--- /dev/null
+++ b/asn1/api/src/main/java/org/apache/directory/api/asn1/util/Asn1Buffer.java
@@ -0,0 +1,153 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+
+package org.apache.directory.api.asn1.util;
+
+/**
+ * A buffer used to store an encoding PDU. It's auto-extended, and
+ * filled by the end.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class Asn1Buffer
+{
+    /** The buffer default size */
+    private static final int DEFAULT_SIZE = 1024;
+
+    /** The current position in the buffer */
+    private int pos = 0;
+
+    /** A buffer to store the encoded PDU */
+    private byte[] buffer = new byte[DEFAULT_SIZE];
+
+
+    /**
+     * @return The current position in the buffer
+     */
+    public int getPos()
+    {
+        return pos;
+    }
+
+
+    /**
+     * Store a byte at the current position in the buffer
+     *
+     * @param b The byte to store
+     */
+    public void put( byte b )
+    {
+        if ( pos == buffer.length )
+        {
+            // The buffer needs to be reallocated, its too small
+            extend( 1 );
+        }
+
+        buffer[buffer.length - pos - 1] = b;
+        pos++;
+    }
+
+
+    /**
+     * Store some bytes at the current position in the buffer
+     *
+     * @param bytes The bytes to store
+     */
+    public void put( byte[] bytes )
+    {
+        if ( pos + bytes.length > buffer.length )
+        {
+            // The buffer needs to be reallocated, its too small
+            extend( bytes.length );
+        }
+
+
+        System.arraycopy( bytes, 0, buffer, buffer.length - ( pos + bytes.length ), bytes.length );
+        pos += bytes.length;
+    }
+
+
+    /**
+     * Extend the buffer
+     */
+    private void extend( int size )
+    {
+        // The buffer needs to be reallocated, it's too small
+        int newSize = ( ( size + buffer.length ) / DEFAULT_SIZE ) * DEFAULT_SIZE;
+
+        if ( size % DEFAULT_SIZE != 0 )
+        {
+            newSize += DEFAULT_SIZE;
+        }
+
+        byte[] newBuffer = new byte[newSize];
+        System.arraycopy( buffer, 0, newBuffer, newSize - buffer.length, buffer.length );
+
+        buffer = newBuffer;
+    }
+
+
+    /**
+     * @return The stored encoded PDU.
+     */
+    public byte[] getBytes()
+    {
+        byte[] result = new byte[pos];
+
+        System.arraycopy( buffer, buffer.length - pos, result, 0, pos );
+
+        return result;
+    }
+
+
+    /**
+     * @return The buffer size (ie the maximum number of bytes that can be
+     * added to this bffder before it gets extended).
+     */
+    public int getSize()
+    {
+        return buffer.length;
+    }
+
+
+    /**
+     * Clear the position, emptying the buffer. If it has grown, reallocate it
+     * to its initial size.
+     */
+    public void clear()
+    {
+        if ( buffer.length > DEFAULT_SIZE )
+        {
+            buffer = new byte[DEFAULT_SIZE];
+        }
+
+        pos = 0;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public String toString()
+    {
+        return "[" + buffer.length + ", " + pos + "] '" + Asn1StringUtils.dumpBytes( buffer ) + '\'';
+    }
+}
diff --git a/asn1/api/src/test/java/org/apache/directory/api/asn1/util/Asn1BufferTest.java b/asn1/api/src/test/java/org/apache/directory/api/asn1/util/Asn1BufferTest.java
new file mode 100644
index 0000000..4a8ee2a
--- /dev/null
+++ b/asn1/api/src/test/java/org/apache/directory/api/asn1/util/Asn1BufferTest.java
@@ -0,0 +1,115 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+package org.apache.directory.api.asn1.util;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Ignore;
+import org.junit.Test;
+
+/**
+ * Test for the Asn1Buffer class
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ */
+public class Asn1BufferTest
+{
+    @Test
+    public void testPutSmallBytes()
+    {
+        Asn1Buffer buffer = new Asn1Buffer();
+
+        for ( int i = 0; i < 512; i++ )
+        {
+            buffer.put( new byte[] { 0x01, ( byte ) i } );
+        }
+
+        assertEquals( 1024, buffer.getPos() );
+        byte[] result = buffer.getBytes();
+
+        for ( int i = 0; i < 512; i++ )
+        {
+            assertEquals( 0x01, result[i + i] );
+            assertEquals( ( byte ) ( 511 - i ), result[ i + i + 1] );
+        }
+    }
+
+
+    @Test
+    public void testPutByte()
+    {
+        Asn1Buffer buffer = new Asn1Buffer();
+
+        for ( int i = 0; i < 1024; i++ )
+        {
+            buffer.put( ( byte ) i );
+        }
+
+        assertEquals( 1024, buffer.getPos() );
+        byte[] result = buffer.getBytes();
+
+        for ( int i = 0; i < 1024; i++ )
+        {
+            assertEquals( ( byte ) ( 1023 - i ), result[i] );
+        }
+    }
+
+
+    @Test
+    public void testPutByteOOB()
+    {
+        Asn1Buffer buffer = new Asn1Buffer();
+
+        for ( int i = 0; i < 1025; i++ )
+        {
+            buffer.put( ( byte ) i );
+        }
+
+        assertEquals( 1025, buffer.getPos() );
+        assertEquals( 2048, buffer.getSize() );
+        byte[] result = buffer.getBytes();
+
+        for ( int i = 0; i < 1025; i++ )
+        {
+            assertEquals( ( byte ) ( 1024 - i ), result[i] );
+        }
+    }
+
+    @Test
+    @Ignore
+    public void testBytesPerf()
+    {
+        long t0 = System.currentTimeMillis();
+
+        for ( int j = 0; j < 1000; j++ )
+        {
+            Asn1Buffer buffer = new Asn1Buffer();
+
+            for ( int i = 0; i < 409600; i++ )
+            {
+                buffer.put( new byte[] { 0x01, ( byte ) i } );
+            }
+        }
+
+        long t1 = System.currentTimeMillis();
+
+        System.out.println( "Delta: " + ( t1 - t0 ) );
+    }
+}