Java OutputStream Write String writeStringAsAsciiBytes(String in, OutputStream out)

Here you can find the source of writeStringAsAsciiBytes(String in, OutputStream out)

Description

Writes the given String string as ASCII bytes (0-127) to the given OutputStream output stream .

License

Apache License

Parameter

Parameter Description
in the given string
out the output stream

Exception

Parameter Description
IOException if an I/O error occurs

Declaration

public static void writeStringAsAsciiBytes(String in, OutputStream out) throws IOException 

Method Source Code


//package com.java2s;
/*/*from   w  w w .  j ava  2 s  .c  o m*/
 * Copyright 2010-2011 the original author or authors.
 *
 * 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.IOException;
import java.io.OutputStream;

public class Main {
    /**
     * Writes the given {@link String string} as ASCII bytes (0-127)
     * to the given {@link OutputStream output stream}.
     * <p>
     * This method ignores the 24 high-order bits of the characters
     * in the given string. It does not check if the characters in
     * the string are indeed ASCII characters or not. It <em>assumes</em>
     * that the given string only contains valid ASCII characters.
     *
     * @param in the given string
     * @param out the output stream
     * @throws IOException if an I/O error occurs
     */
    public static void writeStringAsAsciiBytes(String in, OutputStream out) throws IOException {
        final int length = in.length();
        for (int i = 0; i < length; i++) {
            // The byte to be written is the eight low-order bits
            // of charAt(i). The 24 high-order bits are ignored.
            out.write(in.charAt(i));
        }
    }
}

Related

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