Example usage for java.io OutputStream write

List of usage examples for java.io OutputStream write

Introduction

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

Prototype

public void write(byte b[]) throws IOException 

Source Link

Document

Writes b.length bytes from the specified byte array to this output stream.

Usage

From source file:com.bagdemir.eboda.utils.HttpUtils.java

private static void writeToStream(final OutputStream outputStream, final byte[] content) {
    try {//from  ww  w  .java2 s .  c o m
        outputStream.write(content);
        outputStream.flush();
    } catch (IOException e) {
        LOGGER.error(e);
    } finally {
        try {
            if (outputStream != null) {
                outputStream.close();
            }
        } catch (IOException e) {
            LOGGER.error(e);
        }
    }
}

From source file:ProxyAuthTest.java

private static void writeCmdLine(String cmdLine, OutputStream out) throws Exception {
    out.write(cmdLine.getBytes());
    out.write(System.getProperty("line.separator").getBytes());
}

From source file:ProxyAuthTest.java

private static void writeSqlLine(String stmt, OutputStream out) throws Exception {
    out.write(stmt.getBytes());
    out.write(";".getBytes());
    out.write(System.getProperty("line.separator").getBytes());
}

From source file:FileMonitor.java

/**
 * Copy the source file system structure into the supplied target location. If
 * the source is a file, the destiniation will be created as a file; if the
 * source is a directory, the destination will be created as a directory.
 * //w  w w .j a v  a 2  s  . c o m
 * @param sourceFileOrDirectory
 *          the file or directory whose contents are to be copied into the
 *          target location
 * @param destinationFileOrDirectory
 *          the location where the copy is to be placed; does not need to
 *          exist, but if it does its type must match that of <code>src</code>
 * @return the number of files (not directories) that were copied
 * @throws IllegalArgumentException
 *           if the <code>src</code> or <code>dest</code> references are
 *           null
 * @throws IOException
 */
public static int copy(File sourceFileOrDirectory, File destinationFileOrDirectory) throws IOException {
    int numberOfFilesCopied = 0;
    if (sourceFileOrDirectory.isDirectory()) {
        destinationFileOrDirectory.mkdirs();
        String list[] = sourceFileOrDirectory.list();

        for (int i = 0; i < list.length; i++) {
            String dest1 = destinationFileOrDirectory.getPath() + File.separator + list[i];
            String src1 = sourceFileOrDirectory.getPath() + File.separator + list[i];
            numberOfFilesCopied += copy(new File(src1), new File(dest1));
        }
    } else {
        InputStream fin = new FileInputStream(sourceFileOrDirectory);
        fin = new BufferedInputStream(fin);
        try {
            OutputStream fout = new FileOutputStream(destinationFileOrDirectory);
            fout = new BufferedOutputStream(fout);
            try {
                int c;
                while ((c = fin.read()) >= 0) {
                    fout.write(c);
                }
            } finally {
                fout.close();
            }
        } finally {
            fin.close();
        }
        numberOfFilesCopied++;
    }
    return numberOfFilesCopied;
}

From source file:Main.java

static public void copyDataBase() throws IOException {

    OutputStream databaseOutputStream = new FileOutputStream("/mnt/sdcard/jwdroid.db");
    InputStream databaseInputStream = new FileInputStream("/data/data/com.jwdroid/databases/jwdroid");

    byte[] buffer = new byte[1];
    int length;/*  ww  w  . j  a v a2s. com*/
    while ((length = databaseInputStream.read(buffer)) > 0) {
        databaseOutputStream.write(buffer);
        Log.w("Bytes: ", ((Integer) length).toString());
        Log.w("value", buffer.toString());
    }

    databaseOutputStream.flush();
    databaseOutputStream.close();
    databaseInputStream.close();
}

From source file:net.sf.firemox.xml.XmlModifier.java

/**
 * <ul>/*from   w ww  . j  a v a 2s . c  o m*/
 * Structure of InputStream : Data[size]
 * <li>modifier name [String]</li>
 * <li>live update [boolean]</li>
 * <li>linked to creator [boolean]</li>
 * <li>activationZone [IdZone]</li>
 * <li>activated while this condition [Test]</li>
 * <li>exists until this triggered event [Event[]]</li>
 * <li>layer [integer]</li>
 * </ul>
 * 
 * @param node
 *          the XML card structure
 * @param out
 *          output stream where the card structure will be saved
 */
public static void buildMdbModifier(Node node, OutputStream out) {
    // write the name
    try {
        MToolKit.writeString(out, node.getAttribute("name"));

        // write the "live-update" attribute
        out.write("false".equals(node.getAttribute("live-update")) ? 0 : 1);

        final String activationZone = node.getAttribute("activation-zone");
        if (StringUtils.isBlank(activationZone)) {
            out.write(IdZones.PLAY);
        } else {
            out.write(XmlTools.getZone(activationZone));
        }

        // write the while condition
        final boolean oldValue = XmlTools.defaultOnMeTag;
        XmlTools.defaultOnMeTag = false;
        XmlTest.getTest("test").buildMdb(node.get("while"), out);
        XmlTools.defaultOnMeTag = oldValue;

        // creator link
        String linked = node.getAttribute("linked");
        out.write(linked != null && "true".equals(linked) ? 1 : 0);

        // write the until events
        Node until = node.get("until");
        if (until == null) {
            // permanent attachment (until attached object remove this modifier)
            out.write(0);
        } else {
            until.addAttribute(new XmlParser.Attribute("zone", "play"));
            out.write(until.getNbNodes());
            for (java.lang.Object obj : until) {
                if (obj instanceof Node) {
                    Node child = (Node) obj;
                    if ("text".equals(child.getTag())) {
                        XmlConfiguration
                                .warning("text element in 'until condition' of modifier is not yet implement");
                    } else {
                        XmlEvent.getEvent(child.getTag()).buildMdb(child, out);
                        break;
                    }
                }
            }
        }

        // write the modifier positioning strategy (layer)
        Layer.valueOfXsd(node.getAttribute("layer")).serialize(out);
    } catch (Throwable e2) {
        XmlConfiguration.error(
                "Error found in modifier '" + node.getTag() + "' : " + e2.getMessage() + ". Context=" + node);
    }
}

From source file:com.chenshu.compress.CompressSizeTest.java

public static int compress(StreamFactory factory, byte[] src) {
    byte[] dest = null;

    ByteArrayOutputStream bout = null;
    OutputStream out = null;
    try {//from  ww w  .  j a  va  2  s  .  c om
        bout = new ByteArrayOutputStream(src.length);
        out = factory.getOutputStream(bout);
        out.write(src);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bout != null) {
            try {
                bout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    byte[] bs = bout.toByteArray();
    ByteArrayInputStream bin = null;
    InputStream in = null;
    ByteArrayOutputStream os = null;
    try {
        bin = new ByteArrayInputStream(bs);
        in = factory.getInputStream(bin);
        dest = new byte[src.length];
        os = new ByteArrayOutputStream(src.length);
        int count = 0;
        while ((count = in.read(dest)) != -1) {
            os.write(dest, 0, count);
        }
        dest = os.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (bin != null) {
            try {
                bin.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    System.out.println(src.length == dest.length);
    System.out.println(Arrays.equals(src, dest));
    System.out.println(
            new String(src, Charset.forName("UTF-8")).equals(new String(dest, Charset.forName("UTF-8"))));
    return bs.length;
}