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 2015/03/29 11:24:27 UTC
svn commit: r1669878 - in /directory/shared/trunk/asn1/api/src:
main/java/org/apache/directory/api/asn1/util/Oid.java
test/java/org/apache/directory/api/asn1/util/OidTest.java
Author: elecharny
Date: Sun Mar 29 09:24:26 2015
New Revision: 1669878
URL: http://svn.apache.org/r1669878
Log:
Added missing AL.20 headers
Modified:
directory/shared/trunk/asn1/api/src/main/java/org/apache/directory/api/asn1/util/Oid.java
directory/shared/trunk/asn1/api/src/test/java/org/apache/directory/api/asn1/util/OidTest.java
Modified: directory/shared/trunk/asn1/api/src/main/java/org/apache/directory/api/asn1/util/Oid.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/asn1/api/src/main/java/org/apache/directory/api/asn1/util/Oid.java?rev=1669878&r1=1669877&r2=1669878&view=diff
==============================================================================
--- directory/shared/trunk/asn1/api/src/main/java/org/apache/directory/api/asn1/util/Oid.java (original)
+++ directory/shared/trunk/asn1/api/src/main/java/org/apache/directory/api/asn1/util/Oid.java Sun Mar 29 09:24:26 2015
@@ -1,3 +1,22 @@
+/*
+ * 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;
@@ -8,7 +27,6 @@ import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
-
import org.apache.directory.api.asn1.DecoderException;
import org.apache.directory.api.i18n.I18n;
@@ -56,24 +74,27 @@ import org.apache.directory.api.i18n.I18
*
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
*/
-final public class Oid {
+final public class Oid
+{
private byte[] oidBytes;
private String oidString;
-
-
- private Oid( String oidString, byte[] oidBytes ) {
+
+
+ private Oid( String oidString, byte[] oidBytes )
+ {
this.oidString = oidString;
this.oidBytes = oidBytes;
}
-
-
+
+
@Override
- public boolean equals( Object other ) {
- return (other instanceof Oid)
- && oidString.equals( ((Oid)other).oidString );
+ public boolean equals( Object other )
+ {
+ return ( other instanceof Oid )
+ && oidString.equals( ( ( Oid ) other ).oidString );
}
-
-
+
+
/**
* Decodes an OID from a <code>byte[]</code>.
*
@@ -81,59 +102,64 @@ final public class Oid {
* @return A new Oid
* @throws DecoderException
*/
- public static Oid fromBytes( byte[] oidBytes ) throws DecoderException
+ public static Oid fromBytes( byte[] oidBytes ) throws DecoderException
{
- if ( oidBytes == null || oidBytes.length < 1 )
+ if ( oidBytes == null || oidBytes.length < 1 )
{
throw new DecoderException( I18n.err( I18n.ERR_00033_INVALID_OID, Arrays.toString( oidBytes ) ) );
}
StringBuilder builder = null;
long value = 0;
- for ( int i = 0; i < oidBytes.length; i++ )
+ for ( int i = 0; i < oidBytes.length; i++ )
{
value |= oidBytes[i] & 0x7F;
- if ( oidBytes[i] < 0 )
+ if ( oidBytes[i] < 0 )
{
// leading 1, so value continues
value = value << 7;
}
- else
+ else
{
// value completed
- if ( builder == null ) {
+ if ( builder == null )
+ {
builder = new StringBuilder();
// first value special processing
- if ( value >= 80 ) {
+ if ( value >= 80 )
+ {
// starts with 2
builder.append( 2 );
value = value - 80;
}
- else {
+ else
+ {
// starts with 0 or 1
- long one = value/40;
- long two = value%40;
- if ( one < 0 || one > 2 || two < 0 || (one < 2 && two > 39) )
+ long one = value / 40;
+ long two = value % 40;
+ if ( one < 0 || one > 2 || two < 0 || ( one < 2 && two > 39 ) )
{
- throw new DecoderException( I18n.err( I18n.ERR_00033_INVALID_OID, Arrays.toString( oidBytes ) ) );
+ throw new DecoderException( I18n.err( I18n.ERR_00033_INVALID_OID,
+ Arrays.toString( oidBytes ) ) );
}
- if ( one < 2 )
+ if ( one < 2 )
{
builder.append( one );
value = two;
}
}
}
-
+
// normal processing
builder.append( '.' ).append( value );
value = 0;
}
}
- if ( builder == null ) {
+ if ( builder == null )
+ {
throw new DecoderException( I18n.err( I18n.ERR_00033_INVALID_OID, Arrays.toString( oidBytes ) ) );
}
-
+
return new Oid( builder.toString(), oidBytes );
}
@@ -145,15 +171,16 @@ final public class Oid {
* @return A new Oid
* @throws DecoderException
*/
- public static Oid fromString( String oidString ) throws DecoderException
+ public static Oid fromString( String oidString ) throws DecoderException
{
- if ( oidString == null || oidString.isEmpty() )
+ if ( oidString == null || oidString.isEmpty() )
{
throw new DecoderException( I18n.err( I18n.ERR_00033_INVALID_OID, "" ) );
}
Queue<Long> segments = new LinkedList<Long>();
- for ( String segment : oidString.split( "\\.", -1 ) ) {
+ for ( String segment : oidString.split( "\\.", -1 ) )
+ {
try
{
segments.add( Long.parseLong( segment ) );
@@ -167,46 +194,48 @@ final public class Oid {
// first segment special case
ByteBuffer buffer = new ByteBuffer();
Long segmentOne = segments.poll();
- if ( segmentOne == null || segmentOne < 0 || segmentOne > 2 )
+ if ( segmentOne == null || segmentOne < 0 || segmentOne > 2 )
{
throw new DecoderException( I18n.err( I18n.ERR_00033_INVALID_OID, oidString ) );
}
// second segment special case
Long segment = segments.poll();
- if ( segment == null || segment < 0 || (segmentOne < 2 && segment > 39) )
+ if ( segment == null || segment < 0 || ( segmentOne < 2 && segment > 39 ) )
{
throw new DecoderException( I18n.err( I18n.ERR_00033_INVALID_OID, oidString ) );
}
- buffer.append( (segmentOne * 40) + segment );
-
+ buffer.append( ( segmentOne * 40 ) + segment );
+
// the rest
- while ( (segment = segments.poll()) != null )
+ while ( ( segment = segments.poll() ) != null )
{
buffer.append( segment );
}
return new Oid( oidString, buffer.toByteArray() );
}
-
-
+
+
/**
* Returns the length of the encoded <code>byte[]</code> representation.
*
* @return The length of the byte[]
*/
- public int getEncodedLength() {
+ public int getEncodedLength()
+ {
return oidBytes.length;
}
-
-
+
+
@Override
- public int hashCode() {
+ public int hashCode()
+ {
return oidString.hashCode();
}
-
-
+
+
/**
* Returns true if <code>oidString</code> is a valid string representation
* of an OID. This method simply calls {@link #fromString(String)} and
@@ -219,15 +248,17 @@ final public class Oid {
*/
public static boolean isOid( String oidString )
{
- try {
+ try
+ {
return Oid.fromString( oidString ) != null;
}
- catch ( DecoderException e ) {
+ catch ( DecoderException e )
+ {
return false;
}
}
-
-
+
+
/**
* Returns the <code>byte[]</code> representation of the OID. The
* <code>byte[]</code> that is returned is <i>copied</i> from the internal
@@ -238,23 +269,24 @@ final public class Oid {
*
* @return The encoded <code>byte[]</code> representation of the OID.
*/
- public byte[] toBytes()
+ public byte[] toBytes()
{
return Arrays.copyOf( oidBytes, oidBytes.length );
}
-
+
+
/**
* Returns the string representation of the OID.
*
* @return The string representation of the OID
*/
@Override
- public String toString()
+ public String toString()
{
return oidString;
}
-
-
+
+
/**
* Writes the bytes respresenting this OID to the provided buffer. This
* should be used in preference to the {@link #toBytes()} method in order
@@ -268,7 +300,7 @@ final public class Oid {
buffer.put( oidBytes );
}
-
+
/**
* Writes the bytes respresenting this OID to the provided stream. This
* should be used in preference to the {@link #toBytes()} method in order
@@ -277,24 +309,25 @@ final public class Oid {
* @param outputStream The stream to write the bytes to
* @throws IOException
*/
- public void writeBytesTo( OutputStream outputStream ) throws IOException
+ public void writeBytesTo( OutputStream outputStream ) throws IOException
{
outputStream.write( oidBytes );
}
-
// Internal helper class for converting a long value to a properly encoded
// byte[]
- final private static class ByteBuffer
+ final private static class ByteBuffer
{
private ByteArrayOutputStream buffer = new ByteArrayOutputStream();
-
- public ByteBuffer append( long value )
+
+
+ public ByteBuffer append( long value )
{
write( value, false );
return this;
}
-
+
+
private void write( long value, boolean hasMore )
{
long remaining = value >> 7;
@@ -302,12 +335,14 @@ final public class Oid {
{
write( remaining, true );
}
- buffer.write( hasMore
- ? (byte)((0x7F & value) | 0x80)
- : (byte)(0x7F & value) );
+ buffer.write( hasMore
+ ? ( byte ) ( ( 0x7F & value ) | 0x80 )
+ : ( byte ) ( 0x7F & value ) );
}
-
- public byte[] toByteArray() {
+
+
+ public byte[] toByteArray()
+ {
return buffer.toByteArray();
}
}
Modified: directory/shared/trunk/asn1/api/src/test/java/org/apache/directory/api/asn1/util/OidTest.java
URL: http://svn.apache.org/viewvc/directory/shared/trunk/asn1/api/src/test/java/org/apache/directory/api/asn1/util/OidTest.java?rev=1669878&r1=1669877&r2=1669878&view=diff
==============================================================================
--- directory/shared/trunk/asn1/api/src/test/java/org/apache/directory/api/asn1/util/OidTest.java (original)
+++ directory/shared/trunk/asn1/api/src/test/java/org/apache/directory/api/asn1/util/OidTest.java Sun Mar 29 09:24:26 2015
@@ -1,68 +1,100 @@
+/*
+ * 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 static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
-
import java.util.Arrays;
-
import org.apache.directory.api.asn1.DecoderException;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-public class OidTest {
+
+public class OidTest
+{
private static final Logger logger = LoggerFactory.getLogger( OidTest.class );
-
+
@Test
- public void speed() {
- byte[] bytes = new byte[] { 0x2A, (byte)0x86, 0x48, (byte)0x86, (byte)0xF7, 0x12, 0x01, 0x02, 0x02 };
+ public void speed()
+ {
+ byte[] bytes = new byte[]
+ { 0x2A, ( byte ) 0x86, 0x48, ( byte ) 0x86, ( byte ) 0xF7, 0x12, 0x01, 0x02, 0x02 };
String string = new String( "1.2.840.113554.1.2.2" );
-
+
long start = System.nanoTime();
- for ( int i = 0; i < 1000; i++ ) {
+ for ( int i = 0; i < 1000; i++ )
+ {
Arrays.equals( bytes, bytes );
}
- logger.debug( "byte[]: {}", (System.nanoTime() - start) );
-
+ logger.debug( "byte[]: {}", ( System.nanoTime() - start ) );
+
start = System.nanoTime();
- for ( int i = 0; i < 1000; i++ ) {
+ for ( int i = 0; i < 1000; i++ )
+ {
string.equals( string );
}
- logger.debug( "String: {}", (System.nanoTime() - start) );
+ logger.debug( "String: {}", ( System.nanoTime() - start ) );
}
-
+
+
@Test
- public void fromBytes() throws DecoderException {
+ public void fromBytes() throws DecoderException
+ {
// first byte
- for ( int i = 0; i < 2; i++ )
+ for ( int i = 0; i < 2; i++ )
{ // [0..2]
- for ( int j = 0; j < 40; j++ )
+ for ( int j = 0; j < 40; j++ )
{ // [0..39]
- assertEquals( i + "." + j,
- Oid.fromBytes( new byte[] { (byte)(i*40+j) } )
- .toString() );
+ assertEquals( i + "." + j,
+ Oid.fromBytes( new byte[]
+ { ( byte ) ( i * 40 + j ) } )
+ .toString() );
}
}
-
+
assertEquals( "1.2.840.113554.1.2.2",
- Oid.fromBytes( new byte[] {
- 0x2A, (byte)0x86, 0x48, (byte)0x86, (byte)0xF7, 0x12, 0x01, 0x02, 0x02
- } ).toString() );
+ Oid.fromBytes( new byte[]
+ {
+ 0x2A, ( byte ) 0x86, 0x48, ( byte ) 0x86, ( byte ) 0xF7, 0x12, 0x01, 0x02, 0x02
+ } ).toString() );
assertEquals( "2.123456",
- Oid.fromBytes( new byte[] { (byte)0x87, (byte)0xC5, 0x10 } ).toString() );
+ Oid.fromBytes( new byte[]
+ { ( byte ) 0x87, ( byte ) 0xC5, 0x10 } ).toString() );
}
-
+
+
@Test
- public void test2dot123456() throws DecoderException {
+ public void test2dot123456() throws DecoderException
+ {
String expectedString = "2.123456";
- byte[] expectedBytes = new byte[] { (byte)0x87, (byte)0xC5, 0x10 };
+ byte[] expectedBytes = new byte[]
+ { ( byte ) 0x87, ( byte ) 0xC5, 0x10 };
logger.debug( "b_to_b: " + Arrays.toString( Oid.fromBytes( expectedBytes ).toBytes() ) );
assertTrue( Arrays.equals( expectedBytes, Oid.fromBytes( expectedBytes ).toBytes() ) );
@@ -76,24 +108,29 @@ public class OidTest {
logger.debug( "s_to_s: " + Oid.fromString( expectedString ).toString() );
assertEquals( expectedString, Oid.fromString( expectedString ).toString() );
}
-
+
+
@Test
- public void fromString() throws DecoderException {
+ public void fromString() throws DecoderException
+ {
// first byte
- for ( int i = 0; i < 2; i++ )
+ for ( int i = 0; i < 2; i++ )
{ // [0..2]
- for ( int j = 0; j < 40; j++ )
+ for ( int j = 0; j < 40; j++ )
{ // [0..39]
- assertTrue( Arrays.equals( new byte[] { (byte)(i*40+j) },
- Oid.fromString( i + "." + j ).toBytes() ) );
+ assertTrue( Arrays.equals( new byte[]
+ { ( byte ) ( i * 40 + j ) },
+ Oid.fromString( i + "." + j ).toBytes() ) );
}
}
-
- assertTrue( Arrays.equals(
- new byte[] { 0x2A, (byte)0x86, 0x48, (byte)0x86, (byte)0xF7, 0x12, 0x01, 0x02, 0x02 },
- Oid.fromString( "1.2.840.113554.1.2.2" ).toBytes() ) );
+
+ assertTrue( Arrays.equals(
+ new byte[]
+ { 0x2A, ( byte ) 0x86, 0x48, ( byte ) 0x86, ( byte ) 0xF7, 0x12, 0x01, 0x02, 0x02 },
+ Oid.fromString( "1.2.840.113554.1.2.2" ).toBytes() ) );
}
-
+
+
/**
* Test a null NewOid
*/
@@ -120,7 +157,8 @@ public class OidTest {
{
try
{
- Oid.fromBytes( new byte[] {} );
+ Oid.fromBytes( new byte[]
+ {} );
fail( "Should not reach this point ..." );
}
catch ( DecoderException de )
@@ -139,16 +177,18 @@ public class OidTest {
try
{
Oid oid = null;
-
+
// itu-t(0), recommendation(0), series a-z (0..26)
for ( int i = 1; i < 27; i++ )
{
- oid = Oid.fromBytes( new byte[] { 0x00, ( byte ) i } );
+ oid = Oid.fromBytes( new byte[]
+ { 0x00, ( byte ) i } );
assertEquals( "0.0." + i, oid.toString() );
}
// itu-t(0), question(1)
- oid = Oid.fromBytes( new byte[] { 0x01 } );
+ oid = Oid.fromBytes( new byte[]
+ { 0x01 } );
assertEquals( "0.1", oid.toString() );
// itu-t(0), administration(2), country(202 .. 748)
@@ -196,19 +236,23 @@ public class OidTest {
try
{
// iso(1), standard(0)
- oid = Oid.fromBytes( new byte[] { 40 + 0 } );
+ oid = Oid.fromBytes( new byte[]
+ { 40 + 0 } );
assertEquals( "1.0", oid.toString() );
// iso(1), registration-authority(1)
- oid = Oid.fromBytes( new byte[] { 40 + 1 } );
+ oid = Oid.fromBytes( new byte[]
+ { 40 + 1 } );
assertEquals( "1.1", oid.toString() );
// iso(1), member-body(2)
- oid = Oid.fromBytes( new byte[] { 40 + 2 } );
+ oid = Oid.fromBytes( new byte[]
+ { 40 + 2 } );
assertEquals( "1.2", oid.toString() );
// iso(1), identified-organization(3) | org(3) | organization(3)
- oid = Oid.fromBytes( new byte[] { 40 + 3 } );
+ oid = Oid.fromBytes( new byte[]
+ { 40 + 3 } );
assertEquals( "1.3", oid.toString() );
}
catch ( DecoderException de )
@@ -229,29 +273,35 @@ public class OidTest {
try
{
// joint-iso-itu-t(2), presentation(0)
- oid = Oid.fromBytes( new byte[] { 80 + 0 } );
+ oid = Oid.fromBytes( new byte[]
+ { 80 + 0 } );
assertEquals( "2.0", oid.toString() );
// joint-iso-itu-t(2), asn1(1)
- oid = Oid.fromBytes( new byte[] { 80 + 1 } );
+ oid = Oid.fromBytes( new byte[]
+ { 80 + 1 } );
assertEquals( "2.1", oid.toString() );
// joint-iso-itu-t(2), association-control(2)
- oid = Oid.fromBytes( new byte[] { 80 + 2 } );
+ oid = Oid.fromBytes( new byte[]
+ { 80 + 2 } );
assertEquals( "2.2", oid.toString() );
// joint-iso-itu-t(2), reliable-transfer(3)
- oid = Oid.fromBytes( new byte[] { 80 + 3 } );
+ oid = Oid.fromBytes( new byte[]
+ { 80 + 3 } );
assertEquals( "2.3", oid.toString() );
// ...
// joint-iso-itu-t(2), upu(40)
- oid = Oid.fromBytes( new byte[] { 80 + 40 } );
+ oid = Oid.fromBytes( new byte[]
+ { 80 + 40 } );
assertEquals( "2.40", oid.toString() );
// ...
// joint-iso-itu-t(2), xxx(100)
- oid = Oid.fromBytes( new byte[] { ( byte ) ( 0x81 ), 0x34 } );
+ oid = Oid.fromBytes( new byte[]
+ { ( byte ) ( 0x81 ), 0x34 } );
assertEquals( "2.100", oid.toString() );
}
catch ( DecoderException de )
@@ -322,7 +372,8 @@ public class OidTest {
try
{
- oid = Oid.fromBytes( new byte[] { 0x2b, 0x06, 0x01, 0x05, 0x05, 0x02 } );
+ oid = Oid.fromBytes( new byte[]
+ { 0x2b, 0x06, 0x01, 0x05, 0x05, 0x02 } );
assertEquals( "1.3.6.1.5.5.2", oid.toString() );
}
catch ( DecoderException de )
@@ -373,7 +424,8 @@ public class OidTest {
assertEquals( oid.toString(), oid2.toString() );
oid = Oid.fromString( "0.1.2.3.4" );
- assertTrue( Arrays.equals( new byte[] {0x01, 0x02, 0x03, 0x04}, oid.toBytes() ) );
+ assertTrue( Arrays.equals( new byte[]
+ { 0x01, 0x02, 0x03, 0x04 }, oid.toBytes() ) );
oid2 = Oid.fromBytes( oid.toBytes() );
assertEquals( oid.toString(), oid2.toString() );