Example usage for java.nio.charset Charset decode

List of usage examples for java.nio.charset Charset decode

Introduction

In this page you can find the example usage for java.nio.charset Charset decode.

Prototype

public final CharBuffer decode(ByteBuffer bb) 

Source Link

Document

Convenience method that decodes bytes in this charset into Unicode characters.

Usage

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);//from   w  w w  .j av a  2  s.c  o  m
    Charset csets = Charset.forName("UTF-8");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { (byte) 'j', (byte) 'a', (byte) 'v', (byte) 'a', (byte) '2' });
    System.out.println("Initial Byte Buffer");
    print(bb);/*from  ww  w  .  j  ava2 s.com*/
    Charset csets = Charset.forName("UTF-8");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);/*from w w  w .j a  va  2s .c  o m*/
    Charset csets = Charset.forName("UTF-16BE");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer
            .wrap(new byte[] { (byte) 'j', (byte) 'a', (byte) 'v', (byte) 'a', (byte) '2', (byte) 's' });
    print(bb);/* ww w .j  ava  2 s  . co m*/
    Charset csets = Charset.forName("UTF-16LE");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:Main.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);/*from w  ww  . j av  a 2s.c  o m*/
    Charset csets = Charset.forName("ISO-8859-1");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);/*  w  w w  .j ava2s.  co  m*/
    Charset csets = Charset.forName("US-ASCII");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:MainClass.java

public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, (byte) 'a' });
    System.out.println("Initial Byte Buffer");
    print(bb);/*from  w  w  w.  j  av a  2  s  . com*/
    Charset csets = Charset.forName("UTF-16LE");

    System.out.println(csets.name() + ":");
    print(csets.encode(bb.asCharBuffer()));
    csets.decode(bb);
    bb.rewind();

}

From source file:org.openestate.is24.restapi.examples.RandomRealEstateExample.java

/**
 * Main function./*  w w w .  java 2  s . c  o  m*/
 *
 * @param args
 * command line arguments
 */
public static void main(String[] args) {
    try {
        final RandomRealEstateFactory factory = new RandomRealEstateFactory();
        final Charset charset = Charset.forName("UTF-8");
        for (RandomRealEstateFactory.Type type : RandomRealEstateFactory.Type.values()) {
            System.out.println("----------------------------------------");
            System.out.println("example for " + type + ":");
            RealEstate realEstate = factory.createRandomObject(type);
            ByteArrayOutputStream output = null;
            try {
                output = new ByteArrayOutputStream();
                XmlUtils.writeXml(realEstate, charset.name(), true, output);
                ByteBuffer buffer = ByteBuffer.wrap(output.toByteArray());
                String xml = charset.decode(buffer).toString();
                System.out.println(xml.trim());
            } finally {
                IOUtils.closeQuietly(output);
            }
        }
    } catch (JAXBException ex) {
        System.err.println("XML error!");
        ex.printStackTrace(System.err);
    }
}

From source file:BGrep.java

public static void main(String[] args) {
    String encodingName = "UTF-8"; // Default to UTF-8 encoding
    int flags = Pattern.MULTILINE; // Default regexp flags

    try { // Fatal exceptions are handled after this try block
        // First, process any options
        int nextarg = 0;
        while (args[nextarg].charAt(0) == '-') {
            String option = args[nextarg++];
            if (option.equals("-e")) {
                encodingName = args[nextarg++];
            } else if (option.equals("-i")) { // case-insensitive matching
                flags |= Pattern.CASE_INSENSITIVE;
            } else if (option.equals("-s")) { // Strict Unicode processing
                flags |= Pattern.UNICODE_CASE; // case-insensitive Unicode
                flags |= Pattern.CANON_EQ; // canonicalize Unicode
            } else {
                System.err.println("Unknown option: " + option);
                usage();//ww  w. j a  va 2 s . co m
            }
        }

        // Get the Charset for converting bytes to chars
        Charset charset = Charset.forName(encodingName);

        // Next argument must be a regexp. Compile it to a Pattern object
        Pattern pattern = Pattern.compile(args[nextarg++], flags);

        // Require that at least one file is specified
        if (nextarg == args.length)
            usage();

        // Loop through each of the specified filenames
        while (nextarg < args.length) {
            String filename = args[nextarg++];
            CharBuffer chars; // This will hold complete text of the file
            try { // Handle per-file errors locally
                // Open a FileChannel to the named file
                FileInputStream stream = new FileInputStream(filename);
                FileChannel f = stream.getChannel();

                // Memory-map the file into one big ByteBuffer. This is
                // easy but may be somewhat inefficient for short files.
                ByteBuffer bytes = f.map(FileChannel.MapMode.READ_ONLY, 0, f.size());

                // We can close the file once it is is mapped into memory.
                // Closing the stream closes the channel, too.
                stream.close();

                // Decode the entire ByteBuffer into one big CharBuffer
                chars = charset.decode(bytes);
            } catch (IOException e) { // File not found or other problem
                System.err.println(e); // Print error message
                continue; // and move on to the next file
            }

            // This is the basic regexp loop for finding all matches in a
            // CharSequence. Note that CharBuffer implements CharSequence.
            // A Matcher holds state for a given Pattern and text.
            Matcher matcher = pattern.matcher(chars);
            while (matcher.find()) { // While there are more matches
                // Print out details of the match
                System.out.println(filename + ":" + // file name
                        matcher.start() + ": " + // character pos
                        matcher.group()); // matching text
            }
        }
    }
    // These are the things that can go wrong in the code above
    catch (UnsupportedCharsetException e) { // Bad encoding name
        System.err.println("Unknown encoding: " + encodingName);
    } catch (PatternSyntaxException e) { // Bad pattern
        System.err.println("Syntax error in search pattern:\n" + e.getMessage());
    } catch (ArrayIndexOutOfBoundsException e) { // Wrong number of arguments
        usage();
    }
}

From source file:edu.ku.brc.specify.tools.StrLocalizerApp.java

/**
  * @param args/*from w w w  . jav  a  2  s.co m*/
  */
public static void main(String[] args) {
    setAppName("Specify"); //$NON-NLS-1$
    System.setProperty(AppPreferences.factoryName, "edu.ku.brc.specify.config.AppPrefsDBIOIImpl"); // Needed by AppReferences //$NON-NLS-1$

    for (String s : args) {
        String[] pairs = s.split("="); //$NON-NLS-1$
        if (pairs.length == 2) {
            if (pairs[0].startsWith("-D")) //$NON-NLS-1$
            {
                //System.err.println("["+pairs[0].substring(2, pairs[0].length())+"]["+pairs[1]+"]");
                System.setProperty(pairs[0].substring(2, pairs[0].length()), pairs[1]);
            }
        } else {
            String symbol = pairs[0].substring(2, pairs[0].length());
            //System.err.println("["+symbol+"]");
            System.setProperty(symbol, symbol);
        }
    }

    // Now check the System Properties
    String appDir = System.getProperty("appdir");
    if (StringUtils.isNotEmpty(appDir)) {
        UIRegistry.setDefaultWorkingPath(appDir);
    }

    String appdatadir = System.getProperty("appdatadir");
    if (StringUtils.isNotEmpty(appdatadir)) {
        UIRegistry.setBaseAppDataDir(appdatadir);
    }

    // Then set this
    IconManager.setApplicationClass(Specify.class);
    IconManager.loadIcons(XMLHelper.getConfigDir("icons.xml")); //$NON-NLS-1$
    //ImageIcon icon = IconManager.getIcon("AppIcon", IconManager.IconSize.Std16);

    try {
        ResourceBundle.getBundle("resources", Locale.getDefault()); //$NON-NLS-1$

    } catch (MissingResourceException ex) {
        Locale.setDefault(Locale.ENGLISH);
        UIRegistry.setResourceLocale(Locale.ENGLISH);
    }

    try {
        if (!System.getProperty("os.name").equals("Mac OS X")) {
            UIManager.setLookAndFeel(new Plastic3DLookAndFeel());
            PlasticLookAndFeel.setPlasticTheme(new DesertBlue());
        }
    } catch (Exception e) {
        //whatever
    }
    AppPreferences localPrefs = AppPreferences.getLocalPrefs();
    localPrefs.setDirPath(UIRegistry.getAppDataDir());

    boolean doIt = false;
    if (doIt)

    {
        Charset utf8charset = Charset.forName("UTF-8");
        Charset iso88591charset = Charset.forName("ISO-8859-1");

        ByteBuffer inputBuffer = ByteBuffer.wrap(new byte[] { (byte) 0xC3, (byte) 0xA2 });

        // decode UTF-8
        CharBuffer data = utf8charset.decode(inputBuffer);

        // encode ISO-8559-1
        ByteBuffer outputBuffer = iso88591charset.encode(data);
        byte[] outputData = outputBuffer.array();
        System.out.println(new String(outputData));
        return;
    }

    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            try {
                UIHelper.OSTYPE osType = UIHelper.getOSType();
                if (osType == UIHelper.OSTYPE.Windows) {
                    UIManager.setLookAndFeel(new PlasticLookAndFeel());
                    PlasticLookAndFeel.setPlasticTheme(new ExperienceBlue());

                } else if (osType == UIHelper.OSTYPE.Linux) {
                    UIManager.setLookAndFeel(new PlasticLookAndFeel());
                }
            } catch (Exception e) {
                log.error("Can't change L&F: ", e); //$NON-NLS-1$
            }

            JFrame frame = new JFrame(getResourceString("StrLocalizerApp.AppTitle"));
            frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
            final StrLocalizerApp sl = new StrLocalizerApp();
            sl.addMenuBar(frame);
            frame.setContentPane(sl);
            frame.setSize(768, 1024);

            //Dimension size = frame.getPreferredSize();
            //size.height = 500;
            //frame.setSize(size);
            frame.addWindowListener(sl);
            IconManager.setApplicationClass(Specify.class);
            frame.setIconImage(IconManager.getImage(IconManager.makeIconName("SpecifyWhite32")).getImage());
            UIHelper.centerAndShow(frame);

            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    sl.startUp();
                }
            });
        }
    });
}