Example usage for java.io PrintStream write

List of usage examples for java.io PrintStream write

Introduction

In this page you can find the example usage for java.io PrintStream write.

Prototype

public void write(byte buf[], int off, int len) 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this stream.

Usage

From source file:Main.java

public static void main(String[] args) {
    byte c[] = { 70, 71, 72, 73, 74, 75, 76 };

    PrintStream ps = new PrintStream(System.out);

    // write bytes 1-3
    ps.write(c, 1, 3);

    // flush the stream
    ps.flush();/* ww  w.j  a v  a2s.c  om*/

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    SSLServerSocketFactory ssf = (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
    ServerSocket ss = ssf.createServerSocket(443);
    while (true) {
        Socket s = ss.accept();//from   w w  w.  ja  v  a2 s  . c  o  m
        PrintStream out = new PrintStream(s.getOutputStream());
        BufferedReader in = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String info = null;
        String request = null;
        String refer = null;

        while ((info = in.readLine()) != null) {
            if (info.startsWith("GET")) {
                request = info;
            }
            if (info.startsWith("Referer:")) {
                refer = info;
            }
            if (info.equals(""))
                break;
        }
        if (request != null) {
            out.println("HTTP/1.0 200 OK\nMIME_version:1.0\nContent_Type:text/html");
            int sp1 = request.indexOf(' ');
            int sp2 = request.indexOf(' ', sp1 + 1);
            String filename = request.substring(sp1 + 2, sp2);
            if (refer != null) {
                sp1 = refer.indexOf(' ');
                refer = refer.substring(sp1 + 1, refer.length());
                if (!refer.endsWith("/")) {
                    refer = refer + "/";
                }
                filename = refer + filename;
            }
            URL con = new URL(filename);
            InputStream gotoin = con.openStream();
            int n = gotoin.available();
            byte buf[] = new byte[1024];
            out.println("HTTP/1.0 200 OK\nMIME_version:1.0\nContent_Type:text/html");
            out.println("Content_Length:" + n + "\n");
            while ((n = gotoin.read(buf)) >= 0) {
                out.write(buf, 0, n);
            }
            out.close();
            s.close();
            in.close();
        }
    }
}

From source file:Main.java

private static void pipe(final InputStream src, final PrintStream dest) {

    new Thread(new Runnable() {
        @Override//from w w w. j  av  a2s  .co m
        public void run() {
            try {
                byte[] buffer = new byte[1024];
                for (int n = 0; n != -1; n = src.read(buffer)) {
                    dest.write(buffer, 0, n);
                }
            } catch (IOException e) {
            }
        }
    }).start();
}

From source file:com.sap.prd.mobile.ios.mios.Forker.java

private static void handleLog(final InputStream is, final PrintStream out) throws IOException {

    if (out.checkError())
        throw new IOException(
                "Cannot handle log output. PrintStream that should be used for log handling is damaged.");

    byte[] buff = new byte[1024];
    for (int i; (i = is.read(buff)) != -1;) {
        out.write(buff, 0, i);
        if (out.checkError())
            throw new IOException(
                    "Cannot handle log output from xcodebuild. Underlying PrintStream indicates problems.");
    }/*from www  .  j a  v  a 2s .co  m*/

    out.flush();
}

From source file:uk.co.codezen.maven.composer.mojo.AbstractComposerMojo.java

/**
 * Links source and destination pipes/*w  w  w  .  jav a  2 s .  co m*/
 *
 * @param src Source
 * @param dst Destination
 */
private static void pipe(final InputStream src, final PrintStream dst) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                byte[] buffer = new byte[1024];
                for (int n = 0; n != -1; n = src.read(buffer, 0, 1024)) {
                    dst.write(buffer, 0, n);
                }
            } catch (IOException ex) {
                // exit on error
            }
        }
    }).start();
}

From source file:jetbrains.exodus.util.ForkSupportIO.java

private static Thread createSpinner(final InputStream input, final PrintStream output, final int bufferSize,
        final String title) {
    final Thread result = new Thread(new Runnable() {
        @Override//w  ww.ja v  a  2  s . co m
        public void run() {
            try {
                final byte[] buf = new byte[bufferSize];

                int i;
                while ((i = input.read(buf, 0, bufferSize)) != -1) {
                    output.write(buf, 0, i);
                }
            } catch (IOException ioe) {
                if (log.isWarnEnabled()) {
                    log.warn("IO error in child process for reader: " + input);
                }
            }
        }
    }, title);
    result.start();
    return result;
}

From source file:com.zimbra.cs.imap.ImapMessage.java

private static void nstring(PrintStream ps, String value) {
    if (value == null) {
        ps.write(NIL, 0, 3);
    } else {//from  w ww  .  j a v a 2s  .c om
        astring(ps, value, false);
    }
}

From source file:com.zimbra.cs.imap.ImapMessage.java

private static void nstring2047(PrintStream ps, String value) {
    if (value == null) {
        ps.write(NIL, 0, 3);
        return;/*www.j a  v a 2  s  . co  m*/
    }

    boolean encoded = false;
    for (int i = 0, length = value.length(); i < length; i++) {
        char c = value.charAt(i);
        if (c == '"' || c == '\\' || c >= 0x7f || c < 0x20) {
            encoded = true;
        }
    }
    if (!encoded) {
        ps.write('"');
        ps.print(value);
        ps.write('"');
    } else {
        try {
            // can't use QCodec because it doesn't encode '"', which results in bad quoted-strings
            ps.write('"');
            ps.print(new BCodec().encode(value, "utf-8"));
            ps.write('"');
        } catch (EncoderException ee) {
            ps.write(NIL, 0, 3);
        }
    }
}

From source file:org.roqmessaging.management.HostConfigManager.java

private static void pipe(final InputStream src, final PrintStream dest) {
    new Thread(new Runnable() {
        public void run() {
            try {
                byte[] buffer = new byte[1024];
                for (int n = 0; n != -1; n = src.read(buffer)) {
                    dest.write(buffer, 0, n);
                }/*from  w w w.  j av a 2s.  co  m*/
            } catch (IOException e) { // just exit
            }
        }
    }).start();
}

From source file:io.hops.hopsworks.common.jobs.yarn.LogReader.java

/**
 * Keep calling this till you get a {@link EOFException} for getting logs of
 * all types for a single container./*  w w  w.  j  a  v a  2s  . c  om*/
 *
 * @param valueStream
 * @param out
 * @throws IOException
 */
public static void readAContainerLogsForALogType(DataInputStream valueStream, PrintStream out)
        throws IOException {

    byte[] buf = new byte[65535];

    String fileType = valueStream.readUTF();
    String fileLengthStr = valueStream.readUTF();
    long fileLength = Long.parseLong(fileLengthStr);
    out.print("LogType: ");
    out.println(fileType);
    out.print("LogLength: ");
    out.println(fileLengthStr);
    out.println("Log Contents:");

    long curRead = 0;
    long pendingRead = fileLength - curRead;
    int toRead = pendingRead > buf.length ? buf.length : (int) pendingRead;
    int len = valueStream.read(buf, 0, toRead);
    while (len != -1 && curRead < fileLength) {
        out.write(buf, 0, len);
        curRead += len;

        pendingRead = fileLength - curRead;
        toRead = pendingRead > buf.length ? buf.length : (int) pendingRead;
        len = valueStream.read(buf, 0, toRead);
    }
    out.println("");
}