mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Chris Ridd
03.37.2012 ac276b728eab2faf8021bef1c07ba728f5fff1d9
opendj-sdk/opends/src/dsml/org/opends/dsml/protocol/ByteStringUtility.java
@@ -29,6 +29,10 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CodingErrorAction;
import org.opends.server.types.ByteString;
import org.opends.server.types.ByteStringBuilder;
@@ -36,10 +40,13 @@
/**
 * A utility class to assist in converting DsmlValues (in Objects) into
 * the required ByteStrings.
 * the required ByteStrings, and back again.
 */
public class ByteStringUtility
{
  // Non-lossy UTF-8 converter object.
  private static final Charset UTF8 = Charset.forName("UTF-8");
  /**
   * Returns a ByteString from a DsmlValue Object.
   *
@@ -79,7 +86,10 @@
        }
        finally
        {
          is.close();
          if (is != null)
          {
            is.close();
          }
        }
      }
      else if (obj instanceof Element)
@@ -91,4 +101,27 @@
    return bs;
  }
  /**
   * Returns a DsmlValue (Object) from an LDAP ByteString. The conversion is
   * simplistic - try and convert it to UTF-8 and if that fails return a byte[].
   *
   * @param bs the ByteString returned from LDAP.
   * @return a String or a byte[].
   */
  public static Object convertByteString(ByteString bs)
  {
    try
    {
      CharsetDecoder decoder = UTF8.newDecoder();
      decoder.onMalformedInput(CodingErrorAction.REPORT);
      decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
      decoder.reset();
      CharBuffer chars = decoder.decode(bs.asByteBuffer());
      return chars.toString();
    }
    catch (Exception e)
    {
      return bs.toByteArray();
    }
  }
}