001 /*
002 * Copyright 2008-2016 UnboundID Corp.
003 * All Rights Reserved.
004 */
005 /*
006 * Copyright (C) 2008-2016 UnboundID Corp.
007 *
008 * This program is free software; you can redistribute it and/or modify
009 * it under the terms of the GNU General Public License (GPLv2 only)
010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011 * as published by the Free Software Foundation.
012 *
013 * This program is distributed in the hope that it will be useful,
014 * but WITHOUT ANY WARRANTY; without even the implied warranty of
015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016 * GNU General Public License for more details.
017 *
018 * You should have received a copy of the GNU General Public License
019 * along with this program; if not, see <http://www.gnu.org/licenses>.
020 */
021 package com.unboundid.util;
022
023
024
025 import com.unboundid.asn1.ASN1OctetString;
026
027
028
029 /**
030 * This class provides a mechanism for creating {@link ByteString} values.
031 */
032 @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
033 public final class ByteStringFactory
034 {
035 /**
036 * A pre-allocated ASN.1 octet string with no value.
037 */
038 private static final ASN1OctetString EMPTY_VALUE = new ASN1OctetString();
039
040
041
042 /**
043 * Prevent this class from being instantiated.
044 */
045 private ByteStringFactory()
046 {
047 // No implementation required.
048 }
049
050
051
052 /**
053 * Creates a new byte string with no value.
054 *
055 * @return The created byte string.
056 */
057 public static ByteString create()
058 {
059 return EMPTY_VALUE;
060 }
061
062
063
064 /**
065 * Creates a new byte string with the provided value.
066 *
067 * @param value The value to use for the byte string.
068 *
069 * @return The created byte string.
070 */
071 public static ByteString create(final byte[] value)
072 {
073 return new ASN1OctetString(value);
074 }
075
076
077
078 /**
079 * Creates a new byte string with the provided value.
080 *
081 * @param value The byte array containing the data to use for the value.
082 * It must not be {@code null}.
083 * @param offset The position in the array at which the value begins. It
084 * must be greater than or equal to zero and less or equal to
085 * the end of the array.
086 * @param length The number of bytes contained in the value. It must be
087 * greater than or equal to zero, and the sum of the offset
088 * and the length must be less than or equal to the end of the
089 * array.
090 *
091 * @return The created byte string.
092 */
093 public static ByteString create(final byte[] value, final int offset,
094 final int length)
095 {
096 return new ASN1OctetString(value, offset, length);
097 }
098
099
100
101 /**
102 * Creates a new byte string with the provided value.
103 *
104 * @param value The value to use for the byte string.
105 *
106 * @return The created byte string.
107 */
108 public static ByteString create(final String value)
109 {
110 return new ASN1OctetString(value);
111 }
112 }