Package java.io

Examples of java.io.OutputStream


     * @param skip the number of bytes to skip
     */
    synchronized void send(FileSystem fs, String fileName, long skip) throws IOException {
        connect();
        try {
            OutputStream out = socket.getOutputStream();
            InputStream in = fs.openFileInputStream(fileName);
            IOUtils.skipFully(in, skip);
            IOUtils.copy(in, out);
            in.close();
        } finally {
View Full Code Here


     * @param data the data to send
     */
    synchronized void send(byte[] data) throws IOException {
        connect();
        try {
            OutputStream out = socket.getOutputStream();
            out.write(data);
        } finally {
            socket.close();
        }
        server.trace("closed");
    }
View Full Code Here

      if (clen < Integer.MAX_VALUE)
        res.setContentLength((int) clen);
      else
        res.setHeader("Content-Length", Long.toString(clen));
    }
    OutputStream out = null;
    InputStream in = null;
    try {
      if (!headOnly) {
        out = doCompress ? new GZIPOutputStream(res.getOutputStream()) : (OutputStream) res.getOutputStream();
        // Check throttle.
        if (throttleTab != null) {
          ThrottleItem throttleItem = (ThrottleItem) throttleTab.get(path);
          if (throttleItem != null) {
            // !!! Need to count for multiple simultaneous fetches.
            out = new ThrottledOutputStream(out, throttleItem.getMaxBps());
          }
        }

        in = new FileInputStream(file);
        while (sr > 0) {
          long sl = in.skip(sr);
          if (sl > 0)
            sr -= sl;
          else {
            res.sendError(HttpServletResponse.SC_CONFLICT, "Conflict");
            // better can be Internal Server Error
            return;
          }
        }
        copyStream(in, out, clen);
        if (doCompress)
          ((GZIPOutputStream) out).finish();
      }
    } finally {
      if (in != null)
        try {
          in.close();
        } catch (IOException ioe) {
        }
      if (out != null) {
        out.flush();
        out.close();
      }
    }
  }
View Full Code Here

    }
  }

  public static class DummyPrintStream extends PrintStream {
    public DummyPrintStream() {
      super(new OutputStream() {
        public void write(int i) {
        }
      });
    }
View Full Code Here



  public void sendFrame(String data) throws Exception {
    byte[] bytes = data.getBytes("UTF-8");
    OutputStream outputStream = socket.getOutputStream();
    for (int i = 0; i < bytes.length; i++) {
      outputStream.write(bytes[i]);
    }
    outputStream.flush();
  }
View Full Code Here

                    }
                    break;

                case Event.STATE_TRANSFER_OUTPUTSTREAM:
                    StateTransferInfo sti=(StateTransferInfo)evt.getArg();
                    OutputStream os=sti.outputStream;
                    if(msg_listener instanceof ExtendedMessageListener) {                       
                        if(os != null && msg_listener instanceof ExtendedMessageListener) {
                            if(sti.state_id == null)
                                ((ExtendedMessageListener)msg_listener).getState(os);
                            else
View Full Code Here

        deleteDb("upgrade");
    }

    private void testErrorUpgrading() throws Exception {
        deleteDb("upgrade");
        OutputStream out;
        out = IOUtils.openFileOutputStream(getBaseDir() + "/upgrade.data.db", false);
        out.write(new byte[10000]);
        out.close();
        out = IOUtils.openFileOutputStream(getBaseDir() + "/upgrade.index.db", false);
        out.write(new byte[10000]);
        out.close();
        assertThrows(ErrorCode.FILE_VERSION_ERROR_1, this).
                getConnection("upgrade");

        assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.data.db"));
        assertTrue(IOUtils.exists(getBaseDir() + "/upgrade.index.db"));
View Full Code Here

    private void testTempFile(String fsBase) throws Exception {
        int len = 10000;
        FileSystem fs = FileSystem.getInstance(fsBase);
        String s = fs.createTempFile(fsBase + "/tmp", ".tmp", false, false);
        OutputStream out = fs.openFileOutputStream(s, false);
        byte[] buffer = new byte[len];
        out.write(buffer);
        out.close();
        out = fs.openFileOutputStream(s, true);
        out.write(1);
        out.close();
        InputStream in = fs.openFileInputStream(s);
        for (int i = 0; i < len; i++) {
            assertEquals(0, in.read());
        }
        assertEquals(1, in.read());
        assertEquals(-1, in.read());
        in.close();
        out.close();
        fs.delete(s);
    }
View Full Code Here

        return encoding != null ? encoding : "ISO-8859-1";
    }

    public static void sendFile(HttpServletRequest request, HttpServletResponse response, File file) throws IOException {
        OutputStream out = response.getOutputStream();
        RandomAccessFile raf = new RandomAccessFile(file, "r");
        try {
            long fileSize = raf.length();
            long rangeStart = 0;
            long rangeFinish = fileSize - 1;

            // accept attempts to resume download (if any)
            String range = request.getHeader("Range");
            if (range != null && range.startsWith("bytes=")) {
                String pureRange = range.replaceAll("bytes=", "");
                int rangeSep = pureRange.indexOf("-");

                try {
                    rangeStart = Long.parseLong(pureRange.substring(0, rangeSep));
                    if (rangeStart > fileSize || rangeStart < 0) {
                        rangeStart = 0;
                    }
                } catch (NumberFormatException e) {
                    // ignore the exception, keep rangeStart unchanged
                }

                if (rangeSep < pureRange.length() - 1) {
                    try {
                        rangeFinish = Long.parseLong(pureRange.substring(rangeSep + 1));
                        if (rangeFinish < 0 || rangeFinish >= fileSize) {
                            rangeFinish = fileSize - 1;
                        }
                    } catch (NumberFormatException e) {
                        // ignore the exception
                    }
                }
            }

            // set some headers
            response.setContentType("application/x-download");
            response.setHeader("Content-Disposition", "attachment; filename=" + file.getName());
            response.setHeader("Accept-Ranges", "bytes");
            response.setHeader("Content-Length", Long.toString(rangeFinish - rangeStart + 1));
            response.setHeader("Content-Range", "bytes " + rangeStart + "-" + rangeFinish + "/" + fileSize);

            // seek to the requested offset
            raf.seek(rangeStart);

            // send the file
            byte[] buffer = new byte[4096];

            long len;
            int totalRead = 0;
            boolean nomore = false;
            while (true) {
                len = raf.read(buffer);
                if (len > 0 && totalRead + len > rangeFinish - rangeStart + 1) {
                    // read more then required?
                    // adjust the length
                    len = rangeFinish - rangeStart + 1 - totalRead;
                    nomore = true;
                }

                if (len > 0) {
                    out.write(buffer, 0, (int) len);
                    totalRead += len;
                    if (nomore) {
                        break;
                    }
                } else {
View Full Code Here

        Task task1 = new Task() {
            public void call() throws Exception {
                while (!stop) {
                    Blob b = conn1.createBlob();
                    OutputStream out = b.setBinaryStream(1);
                    out.write(buffer);
                    out.close();
                }
            }
        };
        Task task2 = new Task() {
            public void call() throws Exception {
                while (!stop) {
                    Blob b = conn2.createBlob();
                    OutputStream out = b.setBinaryStream(1);
                    out.write(buffer);
                    out.close();
                }
            }
        };
        task1.execute();
        task2.execute();
View Full Code Here

TOP

Related Classes of java.io.OutputStream

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.