コード例 #1
0
ファイル: Server.java プロジェクト: piyushagade/BitSync
  // Send File
  public void sendFile(String chunkName) throws IOException {
    OutputStream os = null;
    String currentDir = System.getProperty("user.dir");
    chunkName = currentDir + "/src/srcFile/" + chunkName;
    File myFile = new File(chunkName);

    byte[] arrby = new byte[(int) myFile.length()];

    try {
      FileInputStream fis = new FileInputStream(myFile);
      BufferedInputStream bis = new BufferedInputStream(fis);
      bis.read(arrby, 0, arrby.length);

      os = csocket.getOutputStream();
      System.out.println("Sending File.");
      os.write(arrby, 0, arrby.length);
      os.flush();
      System.out.println("File Sent.");
      //			os.close();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      //			os.close();
    }
  }
コード例 #2
0
  public static void copyFile(String filenameIn, String filenameOut, boolean buffer)
      throws IOException {
    long lenIn = new File(filenameIn).length();
    if (debug) System.out.println("read " + filenameIn + " len = " + lenIn);

    InputStream in = new FileInputStream(filenameIn);
    if (buffer) new BufferedInputStream(in, 10000);

    OutputStream out = new FileOutputStream(filenameOut);
    if (buffer) out = new BufferedOutputStream(out, 1000);

    long start = System.currentTimeMillis();
    IO.copyB(in, out, 10000);
    out.flush();
    double took = .001 * (System.currentTimeMillis() - start);

    out.close();
    in.close();

    long lenOut = new File(filenameOut).length();
    if (debug) System.out.println(" write file= " + filenameOut + " len = " + lenOut);

    double rate = lenIn / took / (1000 * 1000);
    String name = buffer ? "buffer" : "no buffer";
    System.out.println(" copy (" + name + ") took = " + took + " sec; rate = " + rate + "Mb/sec");
  }
コード例 #3
0
ファイル: LibRt.java プロジェクト: yamila87/rt_src
 public static int pipe_stream(InputStream is, OutputStream os, boolean wantsKeepOpen)
     throws IOException { // U: copia de un stream al otro
   int cnt = 0;
   int n;
   byte[] buffer = new byte[BUFF_SZ];
   while ((n = is.read(buffer)) > -1) {
     cnt += n;
     os.write(buffer, 0, n);
   }
   if (!wantsKeepOpen) {
     is.close();
     os.close();
   }
   return cnt;
 }
コード例 #4
0
  public void close() {
    if (closed) {
      return;
    }
    closed = true;

    /* close the underlying connection if,
     * a) the streams not set up yet, no response can be sent, or
     * b) if the wrapper output stream is not set up, or
     * c) if the close of the input/outpu stream fails
     */
    try {
      if (uis_orig == null || uos == null) {
        connection.close();
        return;
      }
      if (!uos_orig.isWrapped()) {
        connection.close();
        return;
      }
      if (!uis_orig.isClosed()) {
        uis_orig.close();
      }
      uos.close();
    } catch (IOException e) {
      connection.close();
    }
  }
コード例 #5
0
 public void sendResponseHeaders(int rCode, long contentLen) throws IOException {
   if (sentHeaders) {
     throw new IOException("headers already sent");
   }
   this.rcode = rCode;
   String statusLine = "HTTP/1.1 " + rCode + Code.msg(rCode) + "\r\n";
   OutputStream tmpout = new BufferedOutputStream(ros);
   PlaceholderOutputStream o = getPlaceholderResponseBody();
   tmpout.write(bytes(statusLine, 0), 0, statusLine.length());
   boolean noContentToSend = false; // assume there is content
   rspHdrs.set("Date", df.format(new Date()));
   if (contentLen == 0) {
     if (http10) {
       o.setWrappedStream(new UndefLengthOutputStream(this, ros));
       close = true;
     } else {
       rspHdrs.set("Transfer-encoding", "chunked");
       o.setWrappedStream(new ChunkedOutputStream(this, ros));
     }
   } else {
     if (contentLen == -1) {
       noContentToSend = true;
       contentLen = 0;
     }
     /* content len might already be set, eg to implement HEAD resp */
     if (rspHdrs.getFirst("Content-length") == null) {
       rspHdrs.set("Content-length", Long.toString(contentLen));
     }
     o.setWrappedStream(new FixedLengthOutputStream(this, ros, contentLen));
   }
   write(rspHdrs, tmpout);
   this.rspContentLen = contentLen;
   tmpout.flush();
   tmpout = null;
   sentHeaders = true;
   if (noContentToSend) {
     WriteFinishedEvent e = new WriteFinishedEvent(this);
     server.addEvent(e);
     closed = true;
   }
   server.logReply(rCode, req.requestLine(), null);
 }
コード例 #6
0
 void write(Headers map, OutputStream os) throws IOException {
   Set<Map.Entry<String, List<String>>> entries = map.entrySet();
   for (Map.Entry<String, List<String>> entry : entries) {
     String key = entry.getKey();
     byte[] buf;
     List<String> values = entry.getValue();
     for (String val : values) {
       int i = key.length();
       buf = bytes(key, 2);
       buf[i++] = ':';
       buf[i++] = ' ';
       os.write(buf, 0, i);
       buf = bytes(val, 2);
       i = val.length();
       buf[i++] = '\r';
       buf[i++] = '\n';
       os.write(buf, 0, i);
     }
   }
   os.write('\r');
   os.write('\n');
 }
コード例 #7
0
ファイル: JavaNewIO.java プロジェクト: juniuszhou/java
  public void run() {
    try {

      sc = new Socket();
      InetSocketAddress isa = new InetSocketAddress("localhost", 9999);
      sc.connect(isa);
      OutputStream ost = sc.getOutputStream();

      for (int i = 0; i < 10; ++i) {
        String str = "client interation " + (new Integer(i)).toString();
        ost.write(str.getBytes());
        ost.flush();
        System.out.println("Client at " + (new Integer(i)).toString());
        sleep(1000);
      }
    } catch (Exception e) {
    }
  }
コード例 #8
0
 public void close() throws IOException {
   checkWrap();
   wrapped.close();
 }
コード例 #9
0
 public void flush() throws IOException {
   checkWrap();
   wrapped.flush();
 }
コード例 #10
0
 public void write(byte b[], int off, int len) throws IOException {
   checkWrap();
   wrapped.write(b, off, len);
 }
コード例 #11
0
 public void write(byte b[]) throws IOException {
   checkWrap();
   wrapped.write(b);
 }
コード例 #12
0
ファイル: LibRt.java プロジェクト: yamila87/rt_src
 public static void set_stream(OutputStream os, String data, String encoding) throws IOException {
   os.write(data.getBytes(encoding));
   os.close();
 }