KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > filesystem > local > Convert


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Martin Oberhuber (Wind River) - [170317] add symbolic link support to API
11  *******************************************************************************/

12 package org.eclipse.core.internal.filesystem.local;
13
14 import java.io.UnsupportedEncodingException JavaDoc;
15 import org.eclipse.osgi.service.environment.Constants;
16
17 public class Convert {
18
19     /** Indicates the default string encoding on this platform */
20     private static String JavaDoc defaultEncoding = new java.io.InputStreamReader JavaDoc(new java.io.ByteArrayInputStream JavaDoc(new byte[0])).getEncoding();
21
22     /** Indicates if we are running on windows */
23     private static final boolean isWindows = Constants.OS_WIN32.equals(LocalFileSystem.getOS());
24
25     private static final String JavaDoc WIN32_FILE_PREFIX = "\\\\?\\"; //$NON-NLS-1$
26
private static final String JavaDoc WIN32_UNC_FILE_PREFIX = "\\\\?\\UNC"; //$NON-NLS-1$
27

28     /**
29      * Performs conversion of a long value to a byte array representation.
30      *
31      * @see #bytesToLong(byte[])
32      */

33     public static byte[] longToBytes(long value) {
34
35         // A long value is 8 bytes in length.
36
byte[] bytes = new byte[8];
37
38         // Convert and copy value to byte array:
39
// -- Cast long to a byte to retrieve least significant byte;
40
// -- Left shift long value by 8 bits to isolate next byte to be converted;
41
// -- Repeat until all 8 bytes are converted (long = 64 bits).
42
// Note: In the byte array, the least significant byte of the long is held in
43
// the highest indexed array bucket.
44

45         for (int i = 0; i < bytes.length; i++) {
46             bytes[(bytes.length - 1) - i] = (byte) value;
47             value >>>= 8;
48         }
49
50         return bytes;
51     }
52
53     /**
54      * Performs conversion of a byte array to a long representation.
55      *
56      * @see #longToBytes(long)
57      */

58     public static long bytesToLong(byte[] value) {
59
60         long longValue = 0L;
61
62         // See method convertLongToBytes(long) for algorithm details.
63
for (int i = 0; i < value.length; i++) {
64             // Left shift has no effect through first iteration of loop.
65
longValue <<= 8;
66             longValue ^= value[i] & 0xFF;
67         }
68
69         return longValue;
70     }
71
72     /**
73      * Calling new String(byte[] s) creates a new encoding object and other garbage.
74      * This can be avoided by calling new String(byte[] s, String encoding) instead.
75      * @param source String in platform bytes
76      * @return converted Java String
77      * @since org.eclipse.core.filesystem 1.1
78      */

79     public static String JavaDoc fromPlatformBytes(byte[] source) {
80         if (defaultEncoding == null)
81             return new String JavaDoc(source);
82         // try to use the default encoding
83
try {
84             return new String JavaDoc(source, defaultEncoding);
85         } catch (UnsupportedEncodingException JavaDoc e) {
86             // null the default encoding so we don't try it again
87
defaultEncoding = null;
88             return new String JavaDoc(source);
89         }
90     }
91
92     /**
93      * Calling String.getBytes() creates a new encoding object and other garbage.
94      * This can be avoided by calling String.getBytes(String encoding) instead.
95      */

96     public static byte[] toPlatformBytes(String JavaDoc target) {
97         if (defaultEncoding == null)
98             return target.getBytes();
99         // try to use the default encoding
100
try {
101             return target.getBytes(defaultEncoding);
102         } catch (UnsupportedEncodingException JavaDoc e) {
103             // null the default encoding so we don't try it again
104
defaultEncoding = null;
105             return target.getBytes();
106         }
107     }
108
109     /**
110      * Converts a file name to a unicode char[] suitable for use by native methods.
111      * See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp
112      */

113     public static char[] toPlatformChars(String JavaDoc target) {
114         //Windows use special prefix to handle long filenames
115
if (!isWindows)
116             return target.toCharArray();
117         //convert UNC path of form \\server\path to unicode form \\?\UNC\server\path
118
if (target.startsWith("\\\\")) { //$NON-NLS-1$
119
int nameLength = target.length();
120             int prefixLength = WIN32_UNC_FILE_PREFIX.length();
121             char[] result = new char[prefixLength + nameLength - 1];
122             WIN32_UNC_FILE_PREFIX.getChars(0, prefixLength, result, 0);
123             target.getChars(1, nameLength, result, prefixLength);
124             return result;
125         }
126         //convert simple path of form c:\path to unicode form \\?\c:\path
127
int nameLength = target.length();
128         int prefixLength = WIN32_FILE_PREFIX.length();
129         char[] result = new char[prefixLength + nameLength];
130         WIN32_UNC_FILE_PREFIX.getChars(0, prefixLength, result, 0);
131         target.getChars(0, nameLength, result, prefixLength);
132         return result;
133     }
134 }
135
Popular Tags