Example usage for java.io BufferedInputStream BufferedInputStream

List of usage examples for java.io BufferedInputStream BufferedInputStream

Introduction

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

Prototype

public BufferedInputStream(InputStream in) 

Source Link

Document

Creates a BufferedInputStream and saves its argument, the input stream in, for later use.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {

    String encoding = "ISO-8859-1";
    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();
    String contentType = uc.getContentType();
    int encodingStart = contentType.indexOf("charset=");
    if (encodingStart != -1) {
        encoding = contentType.substring(encodingStart + 8);
    }/*from  w ww . ja  v a 2s  . c  om*/
    InputStream in = new BufferedInputStream(uc.getInputStream());
    Reader r = new InputStreamReader(in, encoding);
    int c;
    while ((c = r.read()) != -1) {
        System.out.print((char) c);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream is = new BufferedInputStream(new FileInputStream("filename.gif"));

    OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));

    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor,
            DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());

    if (factories.length > 0) {
        StreamPrintService service = factories[0].getPrintService(fos);
        DocPrintJob job = service.createPrintJob();
        Doc doc = new SimpleDoc(is, flavor, null);

        PrintJobWatcher pjDone = new PrintJobWatcher(job);

        job.print(doc, null);//from w  w  w. j  a v  a2s . c  o m

        pjDone.waitForDone();
    }

    is.close();
    fos.close();
}

From source file:Redirecting.java

public static void main(String[] args) throws IOException {
    PrintStream console = System.out;
    BufferedInputStream in = new BufferedInputStream(new FileInputStream("Redirecting.java"));
    PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out")));
    System.setIn(in);/*w  w  w .j a  v  a2  s . c  om*/
    System.setOut(out);
    System.setErr(out);
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String s;
    while ((s = br.readLine()) != null)
        System.out.println(s);
    out.close(); // Remember this!
    System.setOut(console);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {

    String hostname = "localhost";

    Socket connection = null;//ww  w .  ja v  a 2s. c  o m
    connection = new Socket(hostname, DEFAULT_PORT);
    Writer out = new OutputStreamWriter(connection.getOutputStream(), "8859_1");
    out.write("\r\n");
    out.flush();
    InputStream raw = connection.getInputStream();
    BufferedInputStream buffer = new BufferedInputStream(raw);
    InputStreamReader in = new InputStreamReader(buffer, "8859_1");
    int c;
    while ((c = in.read()) != -1) {
        if ((c >= 32 && c < 127) || c == '\t' || c == '\r' || c == '\n') {
            System.out.write(c);
        }
    }
    connection.close();

}

From source file:MainClass.java

public static void main(String args[]) throws Exception {

    URL u = new URL("http://www.java2s.com/binary.dat");
    URLConnection uc = u.openConnection();
    String contentType = uc.getContentType();
    int contentLength = uc.getContentLength();
    if (contentType.startsWith("text/") || contentLength == -1) {
        throw new IOException("This is not a binary file.");
    }//ww  w  .  ja va 2  s.  c  o  m
    InputStream raw = uc.getInputStream();
    InputStream in = new BufferedInputStream(raw);
    byte[] data = new byte[contentLength];
    int bytesRead = 0;
    int offset = 0;
    while (offset < contentLength) {
        bytesRead = in.read(data, offset, data.length - offset);
        if (bytesRead == -1)
            break;
        offset += bytesRead;
    }
    in.close();

    if (offset != contentLength) {
        throw new IOException("Only read " + offset + " bytes; Expected " + contentLength + " bytes");
    }

    String filename = u.getFile().substring(filename.lastIndexOf('/') + 1);
    FileOutputStream out = new FileOutputStream(filename);
    out.write(data);
    out.flush();
    out.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    ZipOutputStream fout = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(args[0])));

    for (int n = 1; n < args.length; n++) {

        BufferedInputStream fin = new BufferedInputStream(new FileInputStream(args[n]));
        ZipEntry ze = new ZipEntry(rmPath(args[n]));

        fout.putNextEntry(ze);//from  ww  w  . j a  v a  2 s  .  c om

        int i;
        do {
            i = fin.read();
            if (i != -1)
                fout.write(i);
        } while (i != -1);

        fout.closeEntry();
        fin.close();

        System.out.println("Compressing " + args[n]);
        System.out.println(
                " Original Size: " + ze.getSize() + " Compressed Size: " + ze.getCompressedSize() + "\n");
    }
    fout.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    ZipFile zf = new ZipFile("a.zip");
    Enumeration<? extends ZipEntry> files = zf.entries();

    while (files.hasMoreElements()) {
        ZipEntry ze = files.nextElement();

        System.out.println("Decompressing " + ze.getName());
        System.out.println(/*w w  w.  jav  a  2 s.co m*/
                "  Compressed Size: " + ze.getCompressedSize() + "  Expanded Size: " + ze.getSize() + "\n");

        BufferedInputStream fin = new BufferedInputStream(zf.getInputStream(ze));
        BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(ze.getName()));

        int i;
        do {
            i = fin.read();
            if (i != -1)
                fout.write(i);
        } while (i != -1);

        fout.close();
        fin.close();
    }
    zf.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    OutputStream fos = new BufferedOutputStream(new FileOutputStream("filename.ps"));

    DocFlavor flavor = DocFlavor.INPUT_STREAM.GIF;
    InputStream is = new BufferedInputStream(new FileInputStream("filename.gif"));
    StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor,
            DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType());

    StreamPrintService service = factories[0].getPrintService(fos);
    DocPrintJob job = service.createPrintJob();
    Doc doc = new SimpleDoc(is, flavor, null);

    PrintJobWatcher pjDone = new PrintJobWatcher(job);

    job.print(doc, null);/*w ww  .j  a v a2 s  . c  o m*/

    pjDone.waitForDone();

    is.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    ObjectOutputStream out = new ObjectOutputStream(
            new BufferedOutputStream(new FileOutputStream("file.data")));

    out.writeObject(Calendar.getInstance());
    out.writeObject(new BigDecimal("123.123"));
    out.writeInt(1);//from  ww  w.  j a  v a  2  s. c o m
    out.writeUTF("tutorial");
    out.close();

    ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("file.data")));

    BigDecimal price;
    int unit;
    String desc;

    Calendar date = (Calendar) in.readObject();
    System.out.println(date);

    price = (BigDecimal) in.readObject();
    unit = in.readInt();
    desc = in.readUTF();
    System.out.println(unit);
    System.out.println(desc);
    System.out.println(price);
    in.close();
}

From source file:PrintFile.java

public static void main(String args[]) throws Exception {
    if (args.length != 2) {
        System.err.println("usage : java PrintFile port file");
        System.err.println("sample: java PrintFile LPT1 sample.prn");
        System.exit(-1);//  w w w . j  av a 2  s . com
    }

    String portname = args[0];
    String filename = args[1];

    // Get port
    CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(portname);

    // Open port
    // Requires owner name and timeout
    CommPort port = portId.open("Java Printing", 30000);

    // Setup reading from file
    FileInputStream fis = new FileInputStream(filename);
    BufferedInputStream bis = new BufferedInputStream(fis);

    // Setup output
    OutputStream os = port.getOutputStream();
    BufferedOutputStream bos = new BufferedOutputStream(os);

    int c;
    while ((c = bis.read()) != -1) {
        bos.write(c);
    }

    // Close
    bos.close();
    bis.close();
    port.close();
}