KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > utils > Convert


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.utils;
12
13 import java.io.UnsupportedEncodingException JavaDoc;
14
15 public class Convert {
16
17     /**
18      * Converts the string argument to a byte array.
19      */

20     public static String JavaDoc fromUTF8(byte[] b) {
21         String JavaDoc result;
22         try {
23             result = new String JavaDoc(b, "UTF8"); //$NON-NLS-1$
24
} catch (UnsupportedEncodingException JavaDoc e) {
25             result = new String JavaDoc(b);
26         }
27         return result;
28     }
29
30     /**
31      * Converts the string argument to a byte array.
32      */

33     public static byte[] toUTF8(String JavaDoc s) {
34         byte[] result;
35         try {
36             result = s.getBytes("UTF8"); //$NON-NLS-1$
37
} catch (UnsupportedEncodingException JavaDoc e) {
38             result = s.getBytes();
39         }
40         return result;
41     }
42
43     /**
44      * Performs conversion of a long value to a byte array representation.
45      *
46      * @see #bytesToLong(byte[])
47      */

48     public static byte[] longToBytes(long value) {
49
50         // A long value is 8 bytes in length.
51
byte[] bytes = new byte[8];
52
53         // Convert and copy value to byte array:
54
// -- Cast long to a byte to retrieve least significant byte;
55
// -- Left shift long value by 8 bits to isolate next byte to be converted;
56
// -- Repeat until all 8 bytes are converted (long = 64 bits).
57
// Note: In the byte array, the least significant byte of the long is held in
58
// the highest indexed array bucket.
59

60         for (int i = 0; i < bytes.length; i++) {
61             bytes[(bytes.length - 1) - i] = (byte) value;
62             value >>>= 8;
63         }
64
65         return bytes;
66     }
67
68     /**
69      * Performs conversion of a byte array to a long representation.
70      *
71      * @see #longToBytes(long)
72      */

73     public static long bytesToLong(byte[] value) {
74
75         long longValue = 0L;
76
77         // See method convertLongToBytes(long) for algorithm details.
78
for (int i = 0; i < value.length; i++) {
79             // Left shift has no effect thru first iteration of loop.
80
longValue <<= 8;
81             longValue ^= value[i] & 0xFF;
82         }
83
84         return longValue;
85     }
86 }
87
Popular Tags