Java DataOutput Write String writeString(String s, DataOutput stream)

Here you can find the source of writeString(String s, DataOutput stream)

Description

write String

License

Apache License

Declaration

public static void writeString(String s, DataOutput stream) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w w  w . j av a2  s .  com
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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.*;

public class Main {
    public static void writeString(String s, DataOutput stream) throws IOException {
        if (s == null) {
            stream.writeInt(-1);
            return;
        }
        char[] chars = s.toCharArray();
        byte[] bytes = new byte[chars.length * 2];

        stream.writeInt(chars.length);
        for (int i = 0, i2 = 0; i < chars.length; i++, i2 += 2) {
            char aChar = chars[i];
            bytes[i2] = (byte) ((aChar >>> 8) & 0xFF);
            bytes[i2 + 1] = (byte) ((aChar) & 0xFF);
        }

        stream.write(bytes);
    }

    public static void writeINT(DataOutput record, int val) throws IOException {
        /*
        if (0 <= val && val < 255)
          record.writeByte(val);
        else {
          record.writeByte(255);
          record.writeInt(val);
        }
        */
        if (0 <= val && val < 192) {
            record.writeByte(val);
        } else {
            record.writeByte(192 + (val & 0x3F));
            val >>>= 6;
            while (val >= 128) {
                record.writeByte((val & 0x7F) | 0x80);
                val >>>= 7;
            }
            record.writeByte(val);
        }
    }
}

Related

  1. writeString(final String data, final DataOutput out, final int length)
  2. writeString(final String string, final DataOutput out)
  3. writeString(String par0Str, DataOutput par1DataOutput)
  4. writeString(String s, DataOutput output)
  5. writeString(String s, DataOutput stream)