Java Text File Write writeString(String value)

Here you can find the source of writeString(String value)

Description

Writes a string value to the stream using UTF-8 encoding.

License

Apache License

Parameter

Parameter Description
value the string value to write.

Declaration

public static byte[] writeString(String value) 

Method Source Code

//package com.java2s;
/*//from   ww w  .j  av  a 2s .c  o m
Copyright 2013 Stefano Fratini (mail@stefanofratini.it)
    
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
    
   http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Main {
    private static final int BC_STRING_CHUNK = 'R';
    private static final int BC_STRING_DIRECT = 0x00;
    private static final int STRING_DIRECT_MAX = 0x1f;
    private static final int BC_STRING_SHORT = 0x30;
    private static final int STRING_SHORT_MAX = 0x3ff;

    /**
     * Writes a string value to the stream using UTF-8 encoding.
     * The string will be written with the following syntax:
     *
     * <code><pre>
     * S b16 b8 string-value
     * </pre></code>
     *
     * If the value is null, it will be written as
     *
     * <code><pre>
     * N
     * </pre></code>
     *
     * @param value the string value to write.
     */
    public static byte[] writeString(String value) {

        ByteArrayOutputStream bout = new ByteArrayOutputStream();

        if (value == null) {
            bout.write((byte) 'N');
            return bout.toByteArray();
        }

        int length = value.length();
        int strOffset = 0;

        while (length > 0x8000) {
            int sublen = 0x8000;

            // chunk can't end in high surrogate
            char tail = value.charAt(strOffset + sublen - 1);

            if (0xd800 <= tail && tail <= 0xdbff)
                sublen--;

            bout.write((byte) BC_STRING_CHUNK);
            bout.write((byte) (sublen >> 8));
            bout.write((byte) (sublen));

            try {
                bout.write(printString(value, strOffset, sublen));
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }

            length -= sublen;
            strOffset += sublen;
        }

        if (length <= STRING_DIRECT_MAX) {
            bout.write((byte) (BC_STRING_DIRECT + length));
        } else if (length <= STRING_SHORT_MAX) {
            bout.write((byte) (BC_STRING_SHORT + (length >> 8)));
            bout.write((byte) (length));
        } else {
            bout.write((byte) ('S'));
            bout.write((byte) (length >> 8));
            bout.write((byte) (length));
        }

        try {
            bout.write(printString(value, strOffset, length));
        } catch (IOException ex) {
            throw new RuntimeException(ex);
        }

        return bout.toByteArray();

    }

    /**
     * Prints a string to the stream, encoded as UTF-8
     *
     * @param v the string to print.
     */
    private static byte[] printString(String v, int strOffset, int length) {

        ByteArrayOutputStream bout = new ByteArrayOutputStream();

        for (int i = 0; i < length; i++) {

            char ch = v.charAt(i + strOffset);

            if (ch < 0x80)
                bout.write((byte) (ch));
            else if (ch < 0x800) {
                bout.write((byte) (0xc0 + ((ch >> 6) & 0x1f)));
                bout.write((byte) (0x80 + (ch & 0x3f)));
            } else {
                bout.write((byte) (0xe0 + ((ch >> 12) & 0xf)));
                bout.write((byte) (0x80 + ((ch >> 6) & 0x3f)));
                bout.write((byte) (0x80 + (ch & 0x3f)));
            }
        }

        return bout.toByteArray();
    }
}

Related

  1. writeString(String path, String key, String value)
  2. writeString(String path, String str)
  3. writeString(String path, String str, boolean append)
  4. writeString(String s, File output, String encoding)
  5. writeString(String string, ObjectOutput out)
  6. writeString(StringBuilder out, CharSequence value, boolean escapeXML)
  7. writeStringField(String name, String value, JsonGenerator jgen)
  8. writeStringFile(String FileName, String text)
  9. writeStringInto(String s, File outputFile)