KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > refresh > win32 > Convert


1 /*******************************************************************************
2  * Copyright (c) 2002, 2005 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 - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.resources.refresh.win32;
12
13 import java.io.*;
14
15 /**
16  * Performs character to byte conversion for passing strings to native win32
17  * methods.
18  */

19 public class Convert {
20
21     /*
22      * Obtains the default encoding on this platform
23      */

24     private static String JavaDoc defaultEncoding=
25         new InputStreamReader(new ByteArrayInputStream(new byte[0])).getEncoding();
26
27     /**
28      * Converts the given String to bytes using the platforms default
29      * encoding.
30      *
31      * @param target The String to be converted, can not be <code>null</code>.
32      * @return byte[] The resulting bytes, or <code>null</code>.
33      */

34     /**
35      * Calling String.getBytes() creates a new encoding object and other garbage.
36      * This can be avoided by calling String.getBytes(String encoding) instead.
37      */

38     public static byte[] toPlatformBytes(String JavaDoc target) {
39         if (defaultEncoding == null)
40             return target.getBytes();
41         // try to use the default encoding
42
try {
43             return target.getBytes(defaultEncoding);
44         } catch (UnsupportedEncodingException e) {
45             // null the default encoding so we don't try it again
46
defaultEncoding = null;
47             return target.getBytes();
48         }
49     }
50 }
51
Popular Tags