Java DataOutputStream Write String writeString(DataOutputStream os, String str)

Here you can find the source of writeString(DataOutputStream os, String str)

Description

Writes string into DataOutputStream.

License

Open Source License

Parameter

Parameter Description
os a parameter
str a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void writeString(DataOutputStream os, String str) throws IOException 

Method Source Code

//package com.java2s;
/**//from   w  w  w . j a v  a  2s.c  o m
 * JHeroes CRPG Engine and Game
 * Copyright (C) 2014  Tuomo Untinen
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see http://www.gnu.org/licenses/
 * 
 * 
 * Utilities for handling streams
 */

import java.io.DataOutputStream;
import java.io.IOException;

public class Main {
    /**
     * Writes string into DataOutputStream. First 4 octets tell string length
     * then each characger is written with 2 octets
     * @param os
     * @param str
     * @throws IOException
     */
    public static void writeString(DataOutputStream os, String str) throws IOException {
        if (str != null) {
            os.writeInt(str.length());
            os.writeChars(str);
        } else {
            os.writeInt(0);
        }
    }
}

Related

  1. writeString(DataOutputStream buf, String value)
  2. writeString(DataOutputStream os, String s)
  3. writeString(DataOutputStream out, String s)
  4. writeString(DataOutputStream out, String str)
  5. writeString(DataOutputStream out, String str)
  6. writeString(DataOutputStream out, String str)