001 /*
002 * This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 * This file is licensed to You under the Eclipse Public License (EPL);
005 * You may not use this file except in compliance with the License. You
006 * may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 * See the COPYRIGHT.txt file distributed with this work for information
011 * regarding copyright ownership.
012 */
013 package org.jikesrvm.util;
014
015 public class StringUtilities {
016
017 /**
018 * Convert a <code>String</code> filename to a byte array using the
019 * deprecated ascii String method for cases where passing it through
020 * a character set converter is too heavyweight. Allocates an
021 * additional null-terminator byte so that the resulting byte array
022 * can be passed to native C functions.
023 *
024 * @param fileName File name
025 * @return Byte-array representation
026 */
027 @SuppressWarnings("deprecation")
028 public static byte[] stringToBytesNullTerminated(String fileName) {
029 byte[] asciiName = new byte[fileName.length() + 1]; // +1 for \0 terminator
030 fileName.getBytes(0, fileName.length(), asciiName, 0);
031 return asciiName;
032 }
033
034 /**
035 * Convert a <code>String</code> filename to a byte array using the
036 * deprecated ascii String method for cases where passing it through
037 * a character set converter is too heavyweight.
038 *
039 * @param fileName File name
040 * @return Byte-array representation
041 */
042 @SuppressWarnings("deprecation")
043 public static byte[] stringToBytes(String fileName) {
044 byte[] asciiName = new byte[fileName.length()];
045 fileName.getBytes(0, fileName.length(), asciiName, 0);
046 return asciiName;
047 }
048
049 /**
050 * Convert a byte array to a <code>String</code> assuming the ASCII
051 * character set, for use in cases (such as early in the boot process)
052 * where character set conversion is unavailable or inadvisable.
053 *
054 * @param val Byte array to convert
055 * @return The resulting string
056 */
057 public static String asciiBytesToString(byte[] val) {
058 return asciiBytesToString(val, 0, val.length);
059 }
060
061 /**
062 * Convert a byte array to a <code>String</code> assuming the ASCII
063 * character set, for use in cases (such as early in the boot process)
064 * where character set conversion is unavailable or inadvisable.
065 *
066 * @param val Byte array to convert
067 * @param start Start the string at this byte
068 * @param length Use 'length' bytes
069 * @return The resulting string
070 */
071 @SuppressWarnings("deprecation")
072 public static String asciiBytesToString(byte[] val, int start, int length) {
073 return new String(val, 0, start, length);
074 }
075
076 }