1 12 package org.eclipse.core.internal.filesystem.local; 13 14 import java.io.UnsupportedEncodingException ; 15 import org.eclipse.osgi.service.environment.Constants; 16 17 public class Convert { 18 19 20 private static String defaultEncoding = new java.io.InputStreamReader (new java.io.ByteArrayInputStream (new byte[0])).getEncoding(); 21 22 23 private static final boolean isWindows = Constants.OS_WIN32.equals(LocalFileSystem.getOS()); 24 25 private static final String WIN32_FILE_PREFIX = "\\\\?\\"; private static final String WIN32_UNC_FILE_PREFIX = "\\\\?\\UNC"; 28 33 public static byte[] longToBytes(long value) { 34 35 byte[] bytes = new byte[8]; 37 38 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 58 public static long bytesToLong(byte[] value) { 59 60 long longValue = 0L; 61 62 for (int i = 0; i < value.length; i++) { 64 longValue <<= 8; 66 longValue ^= value[i] & 0xFF; 67 } 68 69 return longValue; 70 } 71 72 79 public static String fromPlatformBytes(byte[] source) { 80 if (defaultEncoding == null) 81 return new String (source); 82 try { 84 return new String (source, defaultEncoding); 85 } catch (UnsupportedEncodingException e) { 86 defaultEncoding = null; 88 return new String (source); 89 } 90 } 91 92 96 public static byte[] toPlatformBytes(String target) { 97 if (defaultEncoding == null) 98 return target.getBytes(); 99 try { 101 return target.getBytes(defaultEncoding); 102 } catch (UnsupportedEncodingException e) { 103 defaultEncoding = null; 105 return target.getBytes(); 106 } 107 } 108 109 113 public static char[] toPlatformChars(String target) { 114 if (!isWindows) 116 return target.toCharArray(); 117 if (target.startsWith("\\\\")) { 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 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 |