Example usage for java.io Writer write

List of usage examples for java.io Writer write

Introduction

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

Prototype

public void write(String str, int off, int len) throws IOException 

Source Link

Document

Writes a portion of a string.

Usage

From source file:com.tri_voltage.md.io.YoutubeVideoParser.java

private static String getStringFromInputStream(String encoding, InputStream instream)
        throws UnsupportedEncodingException, IOException {
    Writer writer = new StringWriter();

    char[] buffer = new char[1024];
    try {/*from   w w  w  .  j  a  v a  2 s  .co  m*/
        Reader reader = new BufferedReader(new InputStreamReader(instream, encoding));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        instream.close();
    }
    String result = writer.toString();
    return result;
}

From source file:com.bluetooth.activities.WiFiControl.java

/**
 * This function converts an InputStream in a String, this is needed to
 * serve the .htm file to the client.//  w  ww . j  a  va 2 s  .  c om
 * <p>
 * To convert the InputStream to String we use the Reader.read(char[]
 * buffer) method. We iterate until the Reader return -1 which means there's
 * no more data to read. We use the StringWriter class to produce the
 * string.
 */
protected static String openHTMLString(Context context, int id) {
    InputStream is = context.getResources().openRawResource(id);
    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return writer.toString();
    } else {
        return "";
    }
}

From source file:eu.anynet.java.util.HttpClient.java

/**
 * Read from inputstream and get the result as string
 * @param stream The stream/*ww w. java 2 s. c o  m*/
 * @param maxlength Max bytes to fetch, set &lt;1 to disable max length
 * @return The string
 * @throws IOException
 */
public static String toString(InputStream stream, int maxlength) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    Writer resultbuffer = new StringWriter();

    char buffer[] = new char[1024];
    int totalReadSize = 0, currentRead = 0, writelength = 0;

    while ((currentRead = reader.read(buffer, 0, buffer.length)) != -1) {
        totalReadSize += currentRead;
        writelength = currentRead;
        if (maxlength > 0 && totalReadSize > maxlength) {
            writelength = maxlength - totalReadSize;
        }

        if (writelength > 0) {
            resultbuffer.write(buffer, 0, writelength);
        } else {
            break;
        }
    }

    reader.close();
    return resultbuffer.toString();
}

From source file:Main.java

public static String getStringFromFile(String pathUrlName, boolean encode) {
    Writer writer = null;
    try {//ww w  .  j  a  v a2  s  .  c  o m
        InputStream is = new FileInputStream(new File(pathUrlName));

        writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader;
            if (encode) {
                reader = new BufferedReader(new InputStreamReader(is, HTML_ENCODING));
            } else {
                reader = new BufferedReader(new InputStreamReader(is));
            }
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {
            is.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return writer.toString();
}

From source file:com.cloudbees.eclipse.core.util.Utils.java

public final static String readString(final InputStream is) throws CloudBeesException {
    if (is == null) {
        return null;
    }/*from  w ww .j ava 2 s.  c  o m*/
    try {
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } catch (Exception e) {
        throw new CloudBeesException("Failed to read inputstream", e);
    }
}

From source file:fr.mby.saml2.sp.impl.helper.SamlHelper.java

/**
 * Decode a SAML2 anthentication request for the HTTP-redirect binding.
 * //w  w  w. j ava  2 s . c  om
 * @param authnRequest
 *            the authn request
 * @return the encoded request
 * @throws IOException
 */
public static String httpRedirectDecode(final String encodedRequest) throws IOException {
    String inflatedRequest = null;

    ByteArrayInputStream bytesIn = null;
    InflaterInputStream inflater = null;

    final byte[] decodedBytes = Base64.decode(encodedRequest);

    try {
        bytesIn = new ByteArrayInputStream(decodedBytes);
        inflater = new InflaterInputStream(bytesIn, new Inflater(true));
        final Writer writer = new StringWriter();
        final char[] buffer = new char[1024];

        final Reader reader = new BufferedReader(new InputStreamReader(inflater, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }

        inflatedRequest = writer.toString();
    } finally {
        if (bytesIn != null) {
            bytesIn.close();
        }
        if (inflater != null) {
            inflater.close();
        }
    }

    return inflatedRequest;
}

From source file:com.glaf.core.util.IOUtils.java

/**
 * write./*ww w  . java2 s.c  o m*/
 * 
 * @param reader
 *            Reader.
 * @param writer
 *            Writer.
 * @param bufferSize
 *            buffer size.
 * @return count.
 * @throws IOException
 */
public static long write(Reader reader, Writer writer, int bufferSize) throws IOException {
    int read;
    long total = 0;
    char[] buf = new char[BUFFER_SIZE];
    while ((read = reader.read(buf)) != -1) {
        writer.write(buf, 0, read);
        total += read;
    }
    return total;
}

From source file:com.serotonin.bacnet4j.util.sero.StreamUtils.java

public static void transfer(Reader reader, Writer writer, long limit) throws IOException {
    char[] buf = new char[1024];
    int readcount;
    long total = 0;
    while ((readcount = reader.read(buf)) != -1) {
        if (limit != -1) {
            if (total + readcount > limit)
                readcount = (int) (limit - total);
        }//from w w w  .  j a  v a2s . c  om

        if (readcount > 0)
            writer.write(buf, 0, readcount);

        total += readcount;
        if (limit != -1 && total >= limit)
            break;
    }
    writer.flush();
}

From source file:Repackage.java

public static void copy(Reader r, Writer w) throws IOException {
    char[] buffer = new char[1024 * 16];

    for (;;) {/*from  ww w . j a v a2 s  . c  o m*/
        int n = r.read(buffer, 0, buffer.length);

        if (n < 0)
            break;

        w.write(buffer, 0, n);
    }
}

From source file:org.zenoss.zep.dao.impl.DaoUtils.java

private static String convertStreamToStr(InputStream is) throws IOException {
    if (is != null) {
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {/*w w w .jav  a 2  s .co  m*/
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer, 0, 1024)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } else {
        return "";
    }
}