Example usage for com.google.common.base Charsets UTF_16BE

List of usage examples for com.google.common.base Charsets UTF_16BE

Introduction

In this page you can find the example usage for com.google.common.base Charsets UTF_16BE.

Prototype

Charset UTF_16BE

To view the source code for com.google.common.base Charsets UTF_16BE.

Click Source Link

Document

UTF-16BE: sixteen-bit UCS Transformation Format, big-endian byte order.

Usage

From source file:com.google.enterprise.connector.common.PdfUtil.java

/**
 * PDF literal strings are limited to 8-bit characters.
 * Unicode characters need to be encoded as UTF-16BE in
 * a stream of hexadecimal characters./*from  w w w  .j  av  a2 s . c  om*/
 *
 * @param text some plain text
 * @return a PDF hexadecimal string encoding the text
 */
public static String toBinaryString(String text) {
    // Leading FEFF indicates big-endian 16-bit Unicode text.
    StringBuilder buf = new StringBuilder("<FEFF");
    byte[] bytes = text.getBytes(Charsets.UTF_16BE);
    Base16.upperCase().encode(bytes, buf);
    buf.append('>');
    return buf.toString();
}

From source file:org.polymap.p4.data.imports.refine.csv.CSVFileImporter.java

@Override
public void createPrompts(IProgressMonitor monitor) throws Exception {
    // charset prompt
    site.newPrompt("encoding").summary.put("Zeichensatz der Daten").description.put(
            "Die Daten knnen bspw. deutsche Umlaute enthalten, die nach dem Hochladen falsch dargestellt werden. "
                    + "Mit dem ndern des Zeichensatzes kann dies korrigiert werden.").extendedUI
                            .put(new PromptUIBuilder() {

                                private String encoding;

                                @Override
                                public void submit(ImporterPrompt prompt) {
                                    formatAndOptions().setEncoding(encoding);
                                    updateOptions();
                                    prompt.ok.set(true);
                                    prompt.value.set(encoding);
                                }/*  w  ww  . j a  v a  2 s .  c o m*/

                                @Override
                                public void createContents(ImporterPrompt prompt, Composite parent,
                                        IPanelToolkit tk) {
                                    // select box
                                    Combo combo = new Combo(parent, SWT.SINGLE);
                                    List<String> encodings = Lists.newArrayList(Charsets.ISO_8859_1.name(),
                                            Charsets.US_ASCII.name(), Charsets.UTF_8.name(),
                                            Charsets.UTF_16.name(), Charsets.UTF_16BE.name(),
                                            Charsets.UTF_16LE.name());

                                    // java.nio.charset.Charset.forName( )
                                    combo.setItems(encodings.toArray(new String[encodings.size()]));
                                    // combo.add

                                    combo.addSelectionListener(new SelectionAdapter() {

                                        @Override
                                        public void widgetSelected(SelectionEvent e) {
                                            Combo c = (Combo) e.getSource();
                                            encoding = encodings.get(c.getSelectionIndex());
                                        }
                                    });
                                    encoding = formatAndOptions().encoding();
                                    int index = encodings.indexOf(encoding);
                                    if (index != -1) {
                                        combo.select(index);
                                    }
                                }
                            });

    site.newPrompt("headline").summary.put("Kopfzeile").description
            .put("Welche Zeile enhlt die Spaltenberschriften?").extendedUI.put(new PromptUIBuilder() {

                private int index;

                @Override
                public void createContents(ImporterPrompt prompt, Composite parent, IPanelToolkit tk) {
                    // TODO use a rhei numberfield here
                    Text text = new Text(parent, SWT.RIGHT);
                    text.setText(formatAndOptions().headerLines());
                    text.addModifyListener(event -> {
                        Text t = (Text) event.getSource();
                        // can throw an exception
                        index = Integer.parseInt(t.getText());
                    });
                    // initial value
                    index = Integer.parseInt(text.getText());
                }

                @Override
                public void submit(ImporterPrompt prompt) {
                    formatAndOptions().setHeaderLines(index);
                    updateOptions();
                    prompt.ok.set(true);
                    prompt.value.set(String.valueOf(index));
                }
            });
}

From source file:com.outerspacecat.util.GrowableCharSequence.java

private void transitionToDisk() throws IOException {
    file = File.createTempFile("gcs", null);
    dst = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), Charsets.UTF_16BE));

    dst.write(buf.toString());//  w ww.  jav  a2 s  .c om
    buf = null;
}

From source file:com.github.spapageo.jannel.msg.Sms.java

/**
 * Constructs and sms using the the most basic information needed
 * @param sender the sender id of the sms
 * @param receiver the receiver of the sms
 * @param msgData the message data of the sms
 * @param smsType the sms type/*from  www .ja  va  2 s  .  co  m*/
 * @param dataCoding the data coding that should be used for this sms
 */
public Sms(String sender, String receiver, String msgData, SmsType smsType, DataCoding dataCoding) {
    this.sender = sender;
    this.receiver = receiver;
    this.msgData = msgData;
    this.smsType = smsType;
    this.coding = dataCoding;
    if (dataCoding.equals(DataCoding.DC_UCS2))
        charset = Charsets.UTF_16BE;
}

From source file:net.minecrell.quartz.mixin.network.MixinLegacyPingHandler.java

private boolean readLegacy(ChannelHandlerContext ctx, ByteBuf buf) {
    if (buf.readUnsignedByte() != 0xFE) {
        return false;
    }// w w  w . j  a  v  a2 s  . c  o m

    MinecraftServer server = this.system.getServer();
    InetSocketAddress client = (InetSocketAddress) ctx.channel().remoteAddress();
    ServerStatusResponse response;

    int i = buf.readableBytes();
    switch (i) {
    case 0:
        logger.debug("Ping: (<=1.3) from {}:{}", client.getAddress(), client.getPort());

        response = QuartzStatusResponse.postLegacy(server, client, QuartzLegacyMinecraftVersion.V1_3, null);
        if (response != null) {
            this.writeResponse(ctx,
                    String.format("%s%d%d", QuartzStatusResponse.getUnformattedMotd(response),
                            response.getPlayers().getOnline(), response.getPlayers().getMax()));
        } else {
            ctx.close();
        }

        break;
    case 1:
        if (buf.readUnsignedByte() != 0x01) {
            return false;
        }

        logger.debug("Ping: (1.4-1.5) from {}:{}", client.getAddress(), client.getPort());

        response = QuartzStatusResponse.postLegacy(server, client, QuartzLegacyMinecraftVersion.V1_5, null);
        if (response != null) {
            this.writeResponse(ctx,
                    String.format("1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d",
                            response.getVersion().getProtocol(), response.getVersion().getName(),
                            QuartzStatusResponse.getMotd(response), response.getPlayers().getOnline(),
                            response.getPlayers().getMax()));
        } else {
            ctx.close();
        }

        break;
    default:
        if (buf.readUnsignedByte() != 0x01 || buf.readUnsignedByte() != 0xFA) {
            return false;
        }
        if (!buf.isReadable(2)) {
            break;
        }
        short length = buf.readShort();
        if (!buf.isReadable(length * 2)) {
            break;
        }
        if (!buf.readBytes(length * 2).toString(Charsets.UTF_16BE).equals("MC|PingHost")) {
            return false;
        }
        if (!buf.isReadable(2)) {
            break;
        }
        length = buf.readShort();
        if (!buf.isReadable(length)) {
            break;
        }

        int protocol = buf.readUnsignedByte();
        length = buf.readShort();
        String host = buf.readBytes(length * 2).toString(Charsets.UTF_16BE);
        int port = buf.readInt();

        logger.debug("Ping: (1.6) from {}:{}", client.getAddress(), client.getPort());

        response = QuartzStatusResponse.postLegacy(server, client,
                new QuartzLegacyMinecraftVersion(QuartzLegacyMinecraftVersion.V1_6, protocol),
                InetSocketAddress.createUnresolved(host, port));
        if (response != null) {
            this.writeResponse(ctx,
                    String.format("1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d",
                            response.getVersion().getProtocol(), response.getVersion().getName(),
                            QuartzStatusResponse.getMotd(response), response.getPlayers().getOnline(),
                            response.getPlayers().getMax()));
        } else {
            ctx.close();
        }

        break;
    }

    return true;
}

From source file:org.spongepowered.mod.mixin.core.status.MixinPingResponseHandler.java

private boolean readLegacy(ChannelHandlerContext ctx, ByteBuf buf) {
    if (buf.readUnsignedByte() != 0xFE) {
        return false;
    }//from w w  w .j  a  v a  2 s  .  c  o m

    MinecraftServer server = this.networkSystem.getServer();
    InetSocketAddress client = (InetSocketAddress) ctx.channel().remoteAddress();
    ServerStatusResponse response;

    int i = buf.readableBytes();
    switch (i) {
    case 0:
        logger.debug("Ping: (<=1.3) from {}:{}", client.getAddress(), client.getPort());

        response = SpongeStatusResponse.postLegacy(server, client, SpongeLegacyMinecraftVersion.V1_3, null);
        if (response != null) {
            this.writeResponse(ctx,
                    String.format("%s%d%d", SpongeStatusResponse.getUnformattedMotd(response),
                            response.getPlayerCountData().getOnlinePlayerCount(),
                            response.getPlayerCountData().getMaxPlayers()));
        } else {
            ctx.close();
        }

        break;
    case 1:
        if (buf.readUnsignedByte() != 0x01) {
            return false;
        }

        logger.debug("Ping: (1.4-1.5) from {}:{}", client.getAddress(), client.getPort());

        response = SpongeStatusResponse.postLegacy(server, client, SpongeLegacyMinecraftVersion.V1_5, null);
        if (response != null) {
            this.writeResponse(ctx,
                    String.format("1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d",
                            response.getProtocolVersionInfo().getProtocol(),
                            response.getProtocolVersionInfo().getName(), SpongeStatusResponse.getMotd(response),
                            response.getPlayerCountData().getOnlinePlayerCount(),
                            response.getPlayerCountData().getMaxPlayers()));
        } else {
            ctx.close();
        }

        break;
    default:
        if (buf.readUnsignedByte() != 0x01 || buf.readUnsignedByte() != 0xFA) {
            return false;
        }
        if (!buf.isReadable(2)) {
            break;
        }
        short length = buf.readShort();
        if (!buf.isReadable(length * 2)) {
            break;
        }
        if (!buf.readBytes(length * 2).toString(Charsets.UTF_16BE).equals("MC|PingHost")) {
            return false;
        }
        if (!buf.isReadable(2)) {
            break;
        }
        length = buf.readShort();
        if (!buf.isReadable(length)) {
            break;
        }

        int protocol = buf.readUnsignedByte();
        length = buf.readShort();
        String host = buf.readBytes(length * 2).toString(Charsets.UTF_16BE);
        int port = buf.readInt();

        logger.debug("Ping: (1.6) from {}:{}", client.getAddress(), client.getPort());

        response = SpongeStatusResponse.postLegacy(server, client,
                new SpongeLegacyMinecraftVersion(SpongeLegacyMinecraftVersion.V1_6, protocol),
                InetSocketAddress.createUnresolved(host, port));
        if (response != null) {
            this.writeResponse(ctx,
                    String.format("1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d",
                            response.getProtocolVersionInfo().getProtocol(),
                            response.getProtocolVersionInfo().getName(), SpongeStatusResponse.getMotd(response),
                            response.getPlayerCountData().getOnlinePlayerCount(),
                            response.getPlayerCountData().getMaxPlayers()));
        } else {
            ctx.close();
        }

        break;
    }

    return true;
}

From source file:org.spongepowered.granite.mixin.status.MixinPingResponseHandler.java

private boolean readLegacy(ChannelHandlerContext ctx, ByteBuf buf) {
    if (buf.readUnsignedByte() != 0xFE) {
        return false;
    }//w w w  .ja v a2 s.co m

    MinecraftServer server = this.networkSystem.getServer();
    InetSocketAddress client = (InetSocketAddress) ctx.channel().remoteAddress();
    ServerStatusResponse response;

    int i = buf.readableBytes();
    switch (i) {
    case 0:
        logger.debug("Ping: (<=1.3) from {}:{}", client.getAddress(), client.getPort());

        response = GraniteStatusResponse.postLegacy(server, client, GraniteLegacyMinecraftVersion.V1_3, null);
        if (response != null) {
            this.writeResponse(ctx,
                    String.format("%s%d%d", GraniteStatusResponse.getUnformattedMotd(response),
                            response.getPlayerCountData().getOnlinePlayerCount(),
                            response.getPlayerCountData().getMaxPlayers()));
        } else {
            ctx.close();
        }

        break;
    case 1:
        if (buf.readUnsignedByte() != 0x01) {
            return false;
        }

        logger.debug("Ping: (1.4-1.5) from {}:{}", client.getAddress(), client.getPort());

        response = GraniteStatusResponse.postLegacy(server, client, GraniteLegacyMinecraftVersion.V1_5, null);
        if (response != null) {
            this.writeResponse(ctx, String.format("1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d",
                    response.getProtocolVersionInfo().getProtocol(),
                    response.getProtocolVersionInfo().getName(), GraniteStatusResponse.getMotd(response),
                    response.getPlayerCountData().getOnlinePlayerCount(),
                    response.getPlayerCountData().getMaxPlayers()));
        } else {
            ctx.close();
        }

        break;
    default:
        if (buf.readUnsignedByte() != 0x01 || buf.readUnsignedByte() != 0xFA) {
            return false;
        }
        if (!buf.isReadable(2)) {
            break;
        }
        short length = buf.readShort();
        if (!buf.isReadable(length * 2)) {
            break;
        }
        if (!buf.readBytes(length * 2).toString(Charsets.UTF_16BE).equals("MC|PingHost")) {
            return false;
        }
        if (!buf.isReadable(2)) {
            break;
        }
        length = buf.readShort();
        if (!buf.isReadable(length)) {
            break;
        }

        int protocol = buf.readUnsignedByte();
        length = buf.readShort();
        String host = buf.readBytes(length * 2).toString(Charsets.UTF_16BE);
        int port = buf.readInt();

        logger.debug("Ping: (1.6) from {}:{}", client.getAddress(), client.getPort());

        response = GraniteStatusResponse.postLegacy(server, client,
                new GraniteLegacyMinecraftVersion(GraniteLegacyMinecraftVersion.V1_6, protocol),
                InetSocketAddress.createUnresolved(host, port));
        if (response != null) {
            this.writeResponse(ctx, String.format("1\u0000%d\u0000%s\u0000%s\u0000%d\u0000%d",
                    response.getProtocolVersionInfo().getProtocol(),
                    response.getProtocolVersionInfo().getName(), GraniteStatusResponse.getMotd(response),
                    response.getPlayerCountData().getOnlinePlayerCount(),
                    response.getPlayerCountData().getMaxPlayers()));
        } else {
            ctx.close();
        }

        break;
    }

    return true;
}

From source file:com.hanhuy.keepassj.SeekOrigin.java

public static Iterable<StrEncodingInfo> getEncodings() {
    if (m_lEncs != null)
        return m_lEncs;

    List<StrEncodingInfo> l = new ArrayList<StrEncodingInfo>();

    l.add(new StrEncodingInfo(StrEncodingType.Ascii, "ASCII", Charsets.US_ASCII, 1, null));
    l.add(new StrEncodingInfo(StrEncodingType.Utf8, "Unicode (UTF-8)", StrUtil.Utf8, 1,
            new byte[] { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF }));
    l.add(new StrEncodingInfo(StrEncodingType.Utf16LE, "Unicode (UTF-16 LE)", Charsets.UTF_16LE, 2,
            new byte[] { (byte) 0xFF, (byte) 0xFE }));
    l.add(new StrEncodingInfo(StrEncodingType.Utf16BE, "Unicode (UTF-16 BE)", Charsets.UTF_16BE, 2,
            new byte[] { (byte) 0xFE, (byte) 0xFF }));
    /*//from   w w  w  .  java 2s .c  om
    l.add(new StrEncodingInfo(StrEncodingType.Utf32LE,
                "Unicode (UTF-32 LE)", new UTF32Encoding(false, false),
                4, new byte[]{0xFF, 0xFE, 0x0, 0x0}));
    l.add(new StrEncodingInfo(StrEncodingType.Utf32BE,
                "Unicode (UTF-32 BE)", new UTF32Encoding(true, false),
                4, new byte[]{0x0, 0x0, 0xFE, 0xFF}));
                */

    m_lEncs = l;
    return l;
}

From source file:com.cloudera.cdk.morphline.shaded.org.apache.commons.codec.binary.binary.StringUtils.java

/**
 * Encodes the given string into a sequence of bytes using the UTF-16BE charset, storing the result into a new byte
 * array./*  ww w  .  ja  va 2  s.com*/
 *
 * @param string
 *            the String to encode, may be {@code null}
 * @return encoded bytes, or {@code null} if the input string was {@code null}
 * @throws NullPointerException
 *             Thrown if {@link Charsets#UTF_16BE} is not initialized, which should never happen since it is
 *             required by the Java platform specification.
 * @since As of 1.7, throws {@link NullPointerException} instead of UnsupportedEncodingException
 * @see <a href="http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html">Standard charsets</a>
 * @see #getBytesUnchecked(String, String)
 */
public static byte[] getBytesUtf16Be(final String string) {
    return getBytes(string, Charsets.UTF_16BE);
}

From source file:org.bimserver.ifc.step.serializer.IfcStepSerializer.java

private void writePrimitive(PrintWriter out, Object val) throws SerializerException {
    if (val instanceof Tristate) {
        Tristate bool = (Tristate) val;
        if (bool == Tristate.TRUE) {
            out.print(BOOLEAN_TRUE);/*w  ww . j a v  a  2 s .c om*/
        } else if (bool == Tristate.FALSE) {
            out.print(BOOLEAN_FALSE);
        } else if (bool == Tristate.UNDEFINED) {
            out.print(BOOLEAN_UNDEFINED);
        }
    } else if (val instanceof Double) {
        if (((Double) val).isInfinite() || (((Double) val).isNaN())) {
            LOGGER.info("Serializing infinite or NaN double as 0.0");
            out.print("0.0");
        } else {
            String string = val.toString();
            if (string.endsWith(DOT_0)) {
                out.print(string.substring(0, string.length() - 1));
            } else {
                out.print(string);
            }
        }
    } else if (val instanceof Boolean) {
        Boolean bool = (Boolean) val;
        if (bool) {
            out.print(BOOLEAN_TRUE);
        } else {
            out.print(BOOLEAN_FALSE);
        }
    } else if (val instanceof String) {
        out.print(SINGLE_QUOTE);
        String stringVal = (String) val;
        for (int i = 0; i < stringVal.length(); i++) {
            char c = stringVal.charAt(i);
            if (c == '\'') {
                out.print("\'\'");
            } else if (c == '\\') {
                out.print("\\\\");
            } else if (c >= 32 && c <= 126) {
                // ISO 8859-1
                out.print(c);
            } else if (c < 255) {
                //  ISO 10646 and ISO 8859-1 are the same < 255 , using ISO_8859_1
                out.write("\\X\\" + new String(
                        Hex.encode(
                                Charsets.ISO_8859_1.encode(CharBuffer.wrap(new char[] { (char) c })).array()),
                        Charsets.UTF_8).toUpperCase());
            } else {
                if (useIso8859_1) {
                    // ISO 8859-1 with -128 offset
                    ByteBuffer encode = Charsets.ISO_8859_1.encode(new String(new char[] { (char) (c - 128) }));
                    out.write("\\S\\" + (char) encode.get());
                } else {
                    // The following code has not been tested (2012-04-25)
                    // Use UCS-2 or UCS-4

                    // TODO when multiple sequential characters should be encoded in UCS-2 or UCS-4, we don't really need to add all those \X0\ \X2\ and \X4\ chars
                    if (Character.isLowSurrogate(c)) {
                        throw new SerializerException("Unexpected low surrogate range char");
                    } else if (Character.isHighSurrogate(c)) {
                        // We need UCS-4, this is probably never happening
                        if (i + 1 < stringVal.length()) {
                            char low = stringVal.charAt(i + 1);
                            if (!Character.isLowSurrogate(low)) {
                                throw new SerializerException(
                                        "High surrogate char should be followed by char in low surrogate range");
                            }
                            try {
                                out.write("\\X4\\" + new String(
                                        Hex.encode(Charset.forName("UTF-32")
                                                .encode(new String(new char[] { c, low })).array()),
                                        Charsets.UTF_8).toUpperCase() + "\\X0\\");
                            } catch (UnsupportedCharsetException e) {
                                throw new SerializerException(e);
                            }
                            i++;
                        } else {
                            throw new SerializerException(
                                    "High surrogate char should be followed by char in low surrogate range, but end of string reached");
                        }
                    } else {
                        // UCS-2 will do
                        out.write("\\X2\\" + new String(
                                Hex.encode(Charsets.UTF_16BE.encode(CharBuffer.wrap(new char[] { c })).array()),
                                Charsets.UTF_8).toUpperCase() + "\\X0\\");
                    }
                }
            }
        }
        out.print(SINGLE_QUOTE);
    } else if (val instanceof Enumerator) {
        out.print("." + val + ".");
    } else {
        out.print(val == null ? "$" : val.toString());
    }
}