Java OutputStream Write String writeString(String value, OutputStream os)

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

Description

Writes a the provided string in a Hessian string representation to the provided output stream using UTF-8 encoding.
The string will be written with the following syntax:
 S b16 b8 string-value 
If the value is null, it will be written as
 N 

License

Apache License

Parameter

Parameter Description
value the string to write to the output string
os the output stream to write the string to

Exception

Parameter Description
IOException an exception

Declaration

private static void writeString(String value, OutputStream os) throws IOException 

Method Source Code


//package com.java2s;
/*//from w w w.j  a  va2  s  . co m
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.IOException;
import java.io.OutputStream;

public class Main {
    /**
     * Writes a the provided string in a Hessian string representation to the provided output 
     * stream using UTF-8 encoding.<br>
     * 
     * 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 to write to the output string
     * @param os the output stream to write the string to
     * 
     * @throws IOException
     */
    private static void writeString(String value, OutputStream os) throws IOException {

        if (value == null) {
            os.write('N');
        } else {
            int length = value.length();
            int offset = 0;

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

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

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

                os.write('s');
                os.write(sublen >> 8);
                os.write(sublen);

                printString(value, offset, sublen, os);

                length -= sublen;
                offset += sublen;
            }

            os.write('S');
            os.write(length >> 8);
            os.write(length);

            printString(value, offset, length, os);
        }
    }

    /**
     * Prints a string (or parts of it) to the provided output stream encoded as UTF-8.<br>
     *
     * @param v the string to print to the output stream
     * @param offset an offset indicating at which character of the string to start
     * @param length the number of characters to write
     * 
     * @throws IOException if an error occurs writing to the output stream
     */
    private static void printString(String v, int offset, int length, OutputStream os) throws IOException {

        for (int i = 0; i < length; i++) {
            char ch = v.charAt(i + offset);

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

Related

  1. WriteString(OutputStream output, String string)
  2. writeString(OutputStream output, String string)
  3. writeString(String data, int length, OutputStream out)
  4. writeString(String s, OutputStream out)
  5. writeString(String str, ObjectOutputStream dos)
  6. writeStringAsAsciiBytes(String in, OutputStream out)
  7. writeStringCompressed(ByteArrayOutputStream baos, String s)
  8. writeStringKey(final Object key, final ObjectOutputStream oos)
  9. writeStringKey(final Object key, final ObjectOutputStream oos)