Advertisement
Guest User

Untitled

a guest
Jan 18th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.64 KB | None | 0 0
  1.  private static String stringFromStandardInput(Charset cs) throws IOException {
  2.         int BUFFER_SIZE = 10;
  3.         ReadableByteChannel in = Channels.newChannel(System.in);
  4.         ByteBuffer bb = ByteBuffer.allocate(10);
  5.         StringBuilder sb = new StringBuilder();
  6.         while(bb.hasRemaining()) {
  7.             if(in.read(bb) > bb.remaining()){
  8.                 BUFFER_SIZE *= 2;
  9.                 ByteBuffer bb_ = ByteBuffer.allocate(BUFFER_SIZE);
  10.                 bb_.put(bb);
  11.                 bb = bb_;
  12.             }
  13.         }
  14.         bb.flip();
  15.         sb.append(cs.decode(bb));
  16.         bb.clear();
  17.         return sb.toString();
  18.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement