public static void readFile(String filenameIn, int bufferSize) throws IOException { long lenIn = new File(filenameIn).length(); if (debug) System.out.println("read " + filenameIn + " len = " + lenIn); InputStream in = new FileInputStream(filenameIn); long total = 0; long start = System.currentTimeMillis(); byte[] buffer = new byte[bufferSize]; while (true) { int bytesRead = in.read(buffer); if (bytesRead == -1) break; total += buffer[0]; } double took = .001 * (System.currentTimeMillis() - start); in.close(); double rate = lenIn / took / (1000 * 1000); System.out.println( " copy (" + bufferSize + ") took = " + took + " sec; rate = " + rate + "Mb/sec total=" + total); }
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"); }
public void start() { try { domainStartTime = System.currentTimeMillis(); messageIO.start(); listener = new TcpServer(listenPort); listener.registerAcceptCallback(this); listener.start(); } catch (Exception e) { Log.exception("DomainServer listener", e); System.exit(1); } }
public static void main(String[] args) throws IOException { System.out.println("Input Stream:"); long start = System.currentTimeMillis(); Path filename = Paths.get(args[0]); long crcValue = checksumInputStream(filename); long end = System.currentTimeMillis(); System.out.println(Long.toHexString(crcValue)); System.out.println((end - start) + " milliseconds"); System.out.println("Buffered Input Stream:"); start = System.currentTimeMillis(); crcValue = checksumBufferedInputStream(filename); end = System.currentTimeMillis(); System.out.println(Long.toHexString(crcValue)); System.out.println((end - start) + " milliseconds"); System.out.println("Random Access File:"); start = System.currentTimeMillis(); crcValue = checksumRandomAccessFile(filename); end = System.currentTimeMillis(); System.out.println(Long.toHexString(crcValue)); System.out.println((end - start) + " milliseconds"); System.out.println("Mapped File:"); start = System.currentTimeMillis(); crcValue = checksumMappedFile(filename); end = System.currentTimeMillis(); System.out.println(Long.toHexString(crcValue)); System.out.println((end - start) + " milliseconds"); }
public static void main(String args[]) throws IOException { ServerSocket sSocket = new ServerSocket(sPort, 10); // Arguments Handling if (args.length != 1) { System.out.println("Must specify a file-path argument."); } else { String currentDir = System.getProperty("user.dir"); filePath = currentDir + "/src/srcFile/" + args[0]; filename = args[0]; } // Get list of chunks owned chunkOwned(); // Call FileSplitter FileSplitter fs = new FileSplitter(); fs.split(filePath); System.out.println("Waiting for connection"); while (true) { Socket connection = sSocket.accept(); System.out.println("Connection received from " + connection.getInetAddress().getHostName()); new Thread(new MultiThreadServer(connection)).start(); } }
/** * Fill the current buffer with bytes from the specified array from the specified offset. * * @param s source array * @param o offset from the beginning of the array * @return number of written bytes */ private int write(final byte[] s, final int o) { final Buffer bf = bm.current(); final int len = Math.min(IO.BLOCKSIZE, s.length - o); System.arraycopy(s, o, bf.data, 0, len); bf.dirty = true; return len; }
void add(Target t) { SocketChannel sc = null; try { sc = SocketChannel.open(); sc.configureBlocking(false); boolean connected = sc.connect(t.address); t.channel = sc; t.connectStart = System.currentTimeMillis(); if (connected) { t.connectFinish = t.connectStart; sc.close(); printer.add(t); } else { synchronized (pending) { pending.add(t); } sel.wakeup(); } } catch (IOException x) { if (sc != null) { try { sc.close(); } catch (IOException xx) { } } t.failure = x; printer.add(t); } }
public DatagramChannel connect(SocketAddress sa) throws IOException { int localPort = 0; synchronized (readLock) { synchronized (writeLock) { synchronized (stateLock) { ensureOpenAndUnconnected(); InetSocketAddress isa = Net.checkAddress(sa); SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkConnect(isa.getAddress().getHostAddress(), isa.getPort()); int n = Net.connect(family, fd, isa.getAddress(), isa.getPort()); if (n <= 0) throw new Error(); // Can't happen // Connection succeeded; disallow further invocation state = ST_CONNECTED; remoteAddress = sa; sender = isa; cachedSenderInetAddress = isa.getAddress(); cachedSenderPort = isa.getPort(); // set or refresh local address localAddress = Net.localAddress(fd); } } } return this; }
@Override public DatagramChannel bind(SocketAddress local) throws IOException { synchronized (readLock) { synchronized (writeLock) { synchronized (stateLock) { ensureOpen(); if (localAddress != null) throw new AlreadyBoundException(); InetSocketAddress isa; if (local == null) { // only Inet4Address allowed with IPv4 socket if (family == StandardProtocolFamily.INET) { isa = new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0); } else { isa = new InetSocketAddress(0); } } else { isa = Net.checkAddress(local); // only Inet4Address allowed with IPv4 socket if (family == StandardProtocolFamily.INET) { InetAddress addr = isa.getAddress(); if (!(addr instanceof Inet4Address)) throw new UnsupportedAddressTypeException(); } } SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkListen(isa.getPort()); } Net.bind(family, fd, isa.getAddress(), isa.getPort()); localAddress = Net.localAddress(fd); } } } return this; }
public void addTarget(Target target) { // 向targets队列中加入一个任务 SocketChannel socketChannel = null; try { socketChannel = SocketChannel.open(); socketChannel.configureBlocking(false); socketChannel.connect(target.address); target.channel = socketChannel; target.connectStart = System.currentTimeMillis(); synchronized (targets) { targets.add(target); } selector.wakeup(); } catch (Exception x) { if (socketChannel != null) { try { socketChannel.close(); } catch (IOException xx) { } } target.failure = x; addFinishedTarget(target); } }
public static void copyFile2null(String filenameIn, int buffer) throws IOException { long lenIn = new File(filenameIn).length(); if (debug) System.out.println("read " + filenameIn + " len = " + lenIn); InputStream in = new FileInputStream(filenameIn); long start = System.currentTimeMillis(); IO.copy2null(in, buffer); double took = .001 * (System.currentTimeMillis() - start); in.close(); double rate = lenIn / took / (1000 * 1000); System.out.println( " copy (" + filenameIn + ") took = " + took + " sec; rate = " + rate + "Mb/sec"); }
// 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(); } }
public static void logInit(boolean force) throws IOException { if (!IsLogInitialized || force) { LogManager.getLogManager() .readConfiguration( LibRt.class.getClassLoader().getResourceAsStream("cfg.logging.properties")); String logFile = System.getProperty("CfgLog"); if (logFile != null) { LogManager.getLogManager().readConfiguration(new FileInputStream(logFile)); } try { LogLvlMax = Integer.parseInt(System.getProperty("LogLvlMax", LogLvlMax + "")); } catch (Exception ex) { } ; // XXX: mostrar mensaje? } }
void send(byte[] data) throws IOException { SocketChannel channel = (SocketChannel) key.channel(); verboseLog("TCP write", data); byte[] lengthArray = new byte[2]; lengthArray[0] = (byte) (data.length >>> 8); lengthArray[1] = (byte) (data.length & 0xFF); ByteBuffer[] buffers = new ByteBuffer[2]; buffers[0] = ByteBuffer.wrap(lengthArray); buffers[1] = ByteBuffer.wrap(data); int nsent = 0; key.interestOps(SelectionKey.OP_WRITE); try { while (nsent < data.length + 2) { if (key.isWritable()) { long n = channel.write(buffers); if (n < 0) throw new EOFException(); nsent += (int) n; if (nsent < data.length + 2 && System.currentTimeMillis() > endTime) throw new SocketTimeoutException(); } else blockUntil(key, endTime); } } finally { if (key.isValid()) key.interestOps(0); } }
/** run */ public void run() { long now = System.currentTimeMillis(); if ((socketCleaner == null) || (now - entry.getLastUse() >= connectionTimeout)) { if (logger.isDebugEnabled()) { logger.debug( "Socket has not been used for " + (now - entry.getLastUse()) + " micro seconds, closing it"); } sockets.remove(entry.getPeerAddress()); try { synchronized (entry) { entry.getSocket().close(); } logger.info("Socket to " + entry.getPeerAddress() + " closed due to timeout"); } catch (IOException ex) { logger.error(ex); } } else { if (logger.isDebugEnabled()) { logger.debug("Scheduling " + ((entry.getLastUse() + connectionTimeout) - now)); } socketCleaner.schedule( new SocketTimeout(entry), (entry.getLastUse() + connectionTimeout) - now); } }
public boolean connect(SocketAddress sa) throws IOException { int trafficClass = 0; // ## Pick up from options int localPort = 0; synchronized (readLock) { synchronized (writeLock) { ensureOpenAndUnconnected(); InetSocketAddress isa = Net.checkAddress(sa); SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkConnect(isa.getAddress().getHostAddress(), isa.getPort()); synchronized (blockingLock()) { int n = 0; try { try { begin(); synchronized (stateLock) { if (!isOpen()) { return false; } readerThread = NativeThread.current(); } for (; ; ) { InetAddress ia = isa.getAddress(); if (ia.isAnyLocalAddress()) ia = InetAddress.getLocalHost(); n = connectImpl(ia, isa.getPort(), trafficClass); if ((n == IOStatus.INTERRUPTED) && isOpen()) continue; break; } } finally { readerCleanup(); end((n > 0) || (n == IOStatus.UNAVAILABLE)); assert IOStatus.check(n); } } catch (IOException x) { // If an exception was thrown, close the channel after // invoking end() so as to avoid bogus // AsynchronousCloseExceptions close(); throw x; } synchronized (stateLock) { remoteAddress = isa; if (n > 0) { // Connection succeeded; disallow further // invocation state = ST_CONNECTED; return true; } // If nonblocking and no exception then connection // pending; disallow another invocation if (!isBlocking()) state = ST_PENDING; else assert false; } } return false; } } }
/* * (non-Javadoc) * * @see * org.apache.mina.core.service.IoHandlerAdapter#sessionIdle(org.apache. * mina.core.session.IoSession, org.apache.mina.core.session.IdleStatus) */ public void sessionIdle(IoSession session, IdleStatus status) throws Exception { if (IdleStatus.BOTH_IDLE.equals(status)) { Long l = (Long) session.getAttribute("last"); if (l != null && System.currentTimeMillis() - l > 60 * 1000) { session.close(true); } } }
public static void runTests() { try { // SHA1 sha1Jmule = new SHA1(); MessageDigest sha1Sun = MessageDigest.getInstance("SHA-1"); SHA1 sha1Gudy = new SHA1(); // SHA1Az shaGudyResume = new SHA1Az(); ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024); File dir = new File(dirname); File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { FileChannel fc = new RandomAccessFile(files[i], "r").getChannel(); System.out.println("Testing " + files[i].getName() + " ..."); while (fc.position() < fc.size()) { fc.read(buffer); buffer.flip(); byte[] raw = new byte[buffer.limit()]; System.arraycopy(buffer.array(), 0, raw, 0, raw.length); sha1Gudy.update(buffer); sha1Gudy.saveState(); ByteBuffer bb = ByteBuffer.wrap(new byte[56081]); sha1Gudy.digest(bb); sha1Gudy.restoreState(); sha1Sun.update(raw); buffer.clear(); } byte[] sun = sha1Sun.digest(); sha1Sun.reset(); byte[] gudy = sha1Gudy.digest(); sha1Gudy.reset(); if (Arrays.equals(sun, gudy)) { System.out.println(" SHA1-Gudy: OK"); } else { System.out.println(" SHA1-Gudy: FAILED"); } buffer.clear(); fc.close(); System.out.println(); } } catch (Throwable e) { Debug.printStackTrace(e); } }
@Override protected void copy(final byte[] entries, final int pre, final int last) { for (int o = 0, i = pre; i < last; ++i, o += IO.NODESIZE) { final int off = cursor(i); final Buffer bf = bm.current(); System.arraycopy(entries, o, bf.data, off, IO.NODESIZE); bf.dirty = true; } }
@Override <A> Future<Void> implConnect( SocketAddress remote, A attachment, CompletionHandler<Void, ? super A> handler) { if (!isOpen()) { Throwable exc = new ClosedChannelException(); if (handler == null) return CompletedFuture.withFailure(exc); Invoker.invoke(this, handler, attachment, null, exc); return null; } InetSocketAddress isa = Net.checkAddress(remote); // permission check SecurityManager sm = System.getSecurityManager(); if (sm != null) sm.checkConnect(isa.getAddress().getHostAddress(), isa.getPort()); // check and update state // ConnectEx requires the socket to be bound to a local address IOException bindException = null; synchronized (stateLock) { if (state == ST_CONNECTED) throw new AlreadyConnectedException(); if (state == ST_PENDING) throw new ConnectionPendingException(); if (localAddress == null) { try { bind(new InetSocketAddress(0)); } catch (IOException x) { bindException = x; } } if (bindException == null) state = ST_PENDING; } // handle bind failure if (bindException != null) { try { close(); } catch (IOException ignore) { } if (handler == null) return CompletedFuture.withFailure(bindException); Invoker.invoke(this, handler, attachment, null, bindException); return null; } // setup task PendingFuture<Void, A> result = new PendingFuture<Void, A>(this, handler, attachment); ConnectTask task = new ConnectTask<A>(isa, result); result.setContext(task); // initiate I/O if (Iocp.supportsThreadAgnosticIo()) { task.run(); } else { Invoker.invokeOnThreadInThreadPool(this, task); } return result; }
void _open() throws IOException { long sleepTime = 100; final long start = System.currentTimeMillis(); while (true) { IOException lastError = null; try { _sock = SocketChannel.open(); _socket = _sock.socket(); _socket.connect(_addr, _options.connectTimeout); _socket.setTcpNoDelay(!USE_NAGLE); _socket.setSoTimeout(_options.socketTimeout); _in = _socket.getInputStream(); return; } catch (IOException ioe) { // TODO - erh to fix lastError = new IOException( "couldn't connect to [" + // _addr + "] bc:" + lastError , lastError ); lastError = new IOException("couldn't connect to [" + _addr + "] bc:" + ioe); _logger.log(Level.INFO, "connect fail to : " + _addr, ioe); } if (!_options.autoConnectRetry || (_pool != null && !_pool._everWorked)) throw lastError; long sleptSoFar = System.currentTimeMillis() - start; if (sleptSoFar >= CONN_RETRY_TIME_MS) throw lastError; if (sleepTime + sleptSoFar > CONN_RETRY_TIME_MS) sleepTime = CONN_RETRY_TIME_MS - sleptSoFar; _logger.severe( "going to sleep and retry. total sleep time after = " + (sleptSoFar + sleptSoFar) + "ms this time:" + sleepTime + "ms"); ThreadUtil.sleep(sleepTime); sleepTime *= 2; } }
public static void copyURL2null(String url, int bufferSize) throws IOException { long start = System.currentTimeMillis(); long count = IO.copyUrlB(url, null, bufferSize); double len = (double) count / (1000 * 1000); double took = .001 * (System.currentTimeMillis() - start); double rate = len / took; System.out.println( " copyURL2null (" + url + ") took = " + took + " sec; len= " + len + " Mbytes; rate = " + Format.d(rate, 3) + " Mb/sec"); }
public static void main(String args[]) { if (args.length != 1) { System.out.println("Usage: java FastStreamCopy filename"); System.exit(1); } NumberFormat digits = NumberFormat.getInstance(); digits.setMaximumFractionDigits(3); long before; long after; double slowTime; double fastTime; double speedUp; String filename = args[0]; String contents; // Slow method System.out.println("Reading file " + args[0] + " using slow method"); before = System.currentTimeMillis(); // Start timing // contents = slowStreamCopy(filename); contents = readFile(filename); after = System.currentTimeMillis(); // End timing slowTime = after - before; // System.out.println("File's contents:\n" + contents); // Fast method System.out.println("Reading file " + args[0] + " using fast method"); before = System.currentTimeMillis(); // Start timing contents = fastStreamCopy(filename); after = System.currentTimeMillis(); // End timing fastTime = after - before; // System.out.println("File's contents:\n" + contents); // Comparison speedUp = 100d * slowTime / fastTime; System.out.println("Slow method required " + slowTime + " ms."); System.out.println("Fast method required " + fastTime + " ms."); System.out.print("Speed up = " + digits.format(speedUp) + "% "); System.out.println(speedUp > 100 ? "Good!" : "Bad!"); }
public static void main(String[] args) throws Exception { if (args.length != 2) { System.out.println("arguments: sourcefile destfile"); System.exit(1); } FileChannel in = new FileInputStream(args[0]).getChannel(), out = new FileOutputStream(args[1]).getChannel(); in.transferTo(0, in.size(), out); // Or: // out.transferFrom(in, 0, in.size()); }
public static void copyFileNIO(String filenameIn, String filenameOut, long kbchunks) throws IOException { FileInputStream in = new FileInputStream(filenameIn); FileChannel inChannel = in.getChannel(); FileOutputStream out = new FileOutputStream(filenameOut); FileChannel outChannel = out.getChannel(); long size = inChannel.size(); // outChannel.position(size-2); // outChannel.write(ByteBuffer.allocate(1)); // outChannel.position(0); if (debug) System.out.println( "read " + filenameIn + " len = " + size + " out starts at=" + outChannel.position()); long start = System.currentTimeMillis(); long done = 0; while (done < size) { long need = Math.min(kbchunks * 1000, size - done); done += inChannel.transferTo(done, need, outChannel); } outChannel.close(); inChannel.close(); double took = .001 * (System.currentTimeMillis() - start); if (debug) System.out.println(" write file= " + filenameOut + " len = " + size); double rate = size / took / (1000 * 1000); System.out.println( " copyFileNIO(" + kbchunks + " kb chunk) took = " + took + " sec; rate = " + rate + "Mb/sec"); }
@Override public void run() { try { out = new ObjectOutputStream(csocket.getOutputStream()); out.flush(); in = new ObjectInputStream(csocket.getInputStream()); // Look for chunks String currentDir = System.getProperty("user.dir"); File folder = new File(currentDir + "/src/srcFile"); File[] listOfFiles = folder.listFiles(); int fileCount = 0; OutputStream os; for (int i = 0; i < listOfFiles.length; i++) { if (listOfFiles[i].isFile() && listOfFiles[i] .getName() .toLowerCase() .contains("chunk")) { // 0 1 10 11 12 13 14 2 20 21 22 23 3 4 5 .... fileCount++; String chunkName = listOfFiles[i].getName().toString(); chunkId = chunkName.substring(chunkName.lastIndexOf('.') + 6); xPayload(chunkId); if ((connTo.equals("2") && Integer.parseInt(chunkId) % noDev == 0) || (connTo.equals("3") && (Integer.parseInt(chunkId) - 1) % noDev == 0) || (connTo.equals("4") && (Integer.parseInt(chunkId) - 2) % noDev == 0) || (connTo.equals("5") && (Integer.parseInt(chunkId) - 3) % noDev == 0) || (connTo.equals("6") && (Integer.parseInt(chunkId) - 4) % noDev == 0)) { System.out.println(chunkName); sendFile(chunkName); } } } xPayload("-1"); System.out.println("All chunks sent."); } catch (IOException ioException) { ioException.printStackTrace(); } finally { // Close connections try { in.close(); out.close(); csocket.close(); System.out.println("Thread closed."); } catch (IOException ioException) { System.out.println("Client " + devId + " disconnected."); } } }
@Override public void close() throws BlockStoreException { try { buffer.force(); if (System.getProperty("os.name").toLowerCase().contains("win")) { log.info("Windows mmap hack: Forcing buffer cleaning"); WindowsMMapHack.forceRelease(buffer); } buffer = null; // Allow it to be GCd and the underlying file mapping to go away. randomAccessFile.close(); } catch (IOException e) { throw new BlockStoreException(e); } }
public static void main(String[] args) { if (args.length < 1) { System.err.println("usage: [jre] event.Speed [client|server]"); } if ("client".equals(args[0])) { time = System.currentTimeMillis(); client(); } else if ("classic-client".equals(args[0])) { classicClient(); } else { server(); } }
public static void copyURL(String url, String filenameOut, int bufferSize) throws IOException { File outFile = new File(filenameOut); long start = System.currentTimeMillis(); String ok = IO.readURLtoFileWithExceptions(url, outFile, bufferSize); double took = .001 * (System.currentTimeMillis() - start); double len = (double) outFile.length() / (1000 * 1000); double rate = len / took; System.out.println( " copyURL (" + url + ") took = " + took + " sec; len= " + len + " Mbytes; rate = " + Format.d(rate, 3) + "Mb/sec ok=" + ok); }
public int send(ByteBuffer src, SocketAddress target) throws IOException { if (src == null) throw new NullPointerException(); synchronized (writeLock) { ensureOpen(); InetSocketAddress isa = Net.checkAddress(target); InetAddress ia = isa.getAddress(); if (ia == null) throw new IOException("Target address not resolved"); synchronized (stateLock) { if (!isConnected()) { if (target == null) throw new NullPointerException(); SecurityManager sm = System.getSecurityManager(); if (sm != null) { if (ia.isMulticastAddress()) { sm.checkMulticast(ia); } else { sm.checkConnect(ia.getHostAddress(), isa.getPort()); } } } else { // Connected case; Check address then write if (!target.equals(remoteAddress)) { throw new IllegalArgumentException("Connected address not equal to target address"); } return write(src); } } int n = 0; try { begin(); if (!isOpen()) return 0; writerThread = NativeThread.current(); do { n = send(fd, src, isa); } while ((n == IOStatus.INTERRUPTED) && isOpen()); synchronized (stateLock) { if (isOpen() && (localAddress == null)) { localAddress = Net.localAddress(fd); } } return IOStatus.normalize(n); } finally { writerThread = 0; end((n > 0) || (n == IOStatus.UNAVAILABLE)); assert IOStatus.check(n); } } }