List of usage examples for java.nio CharBuffer clear
public final Buffer clear()
From source file:MainClass.java
public static void main(String[] argv) throws Exception { CharBuffer buffer = CharBuffer.wrap("01234567"); buffer.position(3).limit(6).mark().position(5); CharBuffer dupeBuffer = buffer.duplicate(); buffer.clear(); println(buffer);/*from w w w .j av a 2s. c o m*/ println(dupeBuffer); dupeBuffer.reset(); println(dupeBuffer); dupeBuffer.clear(); println(dupeBuffer); }
From source file:MainClass.java
public static void main(String[] argv) throws Exception { char[] chars = new char[60]; CharBuffer cb = CharBuffer.wrap(chars, 12, 42); println(cb);//ww w .j a v a 2 s.co m cb.put("This is a test String"); cb.flip(); println(cb); cb.clear(); cb.put("Foobar"); println(cb); for (int i = 0; i < 20; i++) { System.out.println("[" + i + "] = " + chars[i]); } }
From source file:MainClass.java
License:asdf
public static void main(String[] argv) throws Exception { CharBuffer buffer = CharBuffer.allocate(100); String string = "asdf"; for (int i = 0; i < string.length(); i++) { buffer.put(string.charAt(i));/*w w w . j a v a 2s . c o m*/ } buffer.flip(); drainBuffer(buffer); buffer.clear(); }
From source file:MainClass.java
public static void main(String[] args) { long[] primes = new long[] { 1, 2, 3, 5, 7 }; File aFile = new File("C:/test/primes.txt"); FileOutputStream outputFile = null; try {/*from www . java 2 s. co m*/ outputFile = new FileOutputStream(aFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel file = outputFile.getChannel(); final int BUFFERSIZE = 100; ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE); DoubleBuffer doubleBuf = buf.asDoubleBuffer(); buf.position(8); CharBuffer charBuf = buf.asCharBuffer(); for (long prime : primes) { String primeStr = "prime = " + prime; doubleBuf.put(0, (double) primeStr.length()); charBuf.put(primeStr); buf.position(2 * charBuf.position() + 8); LongBuffer longBuf = buf.asLongBuffer(); longBuf.put(prime); buf.position(buf.position() + 8); buf.flip(); try { file.write(buf); } catch (IOException e) { e.printStackTrace(System.err); } buf.clear(); doubleBuf.clear(); charBuf.clear(); } try { System.out.println("File written is " + file.size() + "bytes."); outputFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:GetWebPageDemo.java
public static void main(String args[]) throws Exception { String resource, host, file;//from w w w .ja va 2 s. co m int slashPos; resource = "www.java2s.com/index.htm"; slashPos = resource.indexOf('/'); // find host/file separator if (slashPos < 0) { resource = resource + "/"; slashPos = resource.indexOf('/'); } file = resource.substring(slashPos); // isolate host and file parts host = resource.substring(0, slashPos); System.out.println("Host to contact: '" + host + "'"); System.out.println("File to fetch : '" + file + "'"); SocketChannel channel = null; try { Charset charset = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = charset.newDecoder(); CharsetEncoder encoder = charset.newEncoder(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); CharBuffer charBuffer = CharBuffer.allocate(1024); InetSocketAddress socketAddress = new InetSocketAddress(host, 80); channel = SocketChannel.open(); channel.connect(socketAddress); String request = "GET " + file + " \r\n\r\n"; channel.write(encoder.encode(CharBuffer.wrap(request))); while ((channel.read(buffer)) != -1) { buffer.flip(); decoder.decode(buffer, charBuffer, false); charBuffer.flip(); System.out.println(charBuffer); buffer.clear(); charBuffer.clear(); } } catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } finally { if (channel != null) { try { channel.close(); } catch (IOException ignored) { } } } System.out.println("\nDone."); }
From source file:MainClass.java
public static void main(String[] args) { String[] phrases = { "A", "B 1", "C 1.3" }; String dirname = "C:/test"; String filename = "Phrases.txt"; File dir = new File(dirname); File aFile = new File(dir, filename); FileOutputStream outputFile = null; try {//from w ww .jav a 2 s .c o m outputFile = new FileOutputStream(aFile, true); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel outChannel = outputFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(1024); System.out.println(buf.position()); System.out.println(buf.limit()); System.out.println(buf.capacity()); CharBuffer charBuf = buf.asCharBuffer(); System.out.println(charBuf.position()); System.out.println(charBuf.limit()); System.out.println(charBuf.capacity()); Formatter formatter = new Formatter(charBuf); int number = 0; for (String phrase : phrases) { formatter.format("%n %s", ++number, phrase); System.out.println(charBuf.position()); System.out.println(charBuf.limit()); System.out.println(charBuf.capacity()); charBuf.flip(); System.out.println(charBuf.position()); System.out.println(charBuf.limit()); System.out.println(charBuf.length()); buf.limit(2 * charBuf.length()); // Set byte buffer limit System.out.println(buf.position()); System.out.println(buf.limit()); System.out.println(buf.remaining()); try { outChannel.write(buf); buf.clear(); charBuf.clear(); } catch (IOException e) { e.printStackTrace(System.err); } } try { outputFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:GetWebPageApp.java
public static void main(String args[]) throws Exception { String resource, host, file;//from w ww . ja va 2s.co m int slashPos; resource = "www.java2s.com/index.htm"; // skip HTTP:// slashPos = resource.indexOf('/'); // find host/file separator if (slashPos < 0) { resource = resource + "/"; slashPos = resource.indexOf('/'); } file = resource.substring(slashPos); // isolate host and file parts host = resource.substring(0, slashPos); System.out.println("Host to contact: '" + host + "'"); System.out.println("File to fetch : '" + file + "'"); SocketChannel channel = null; try { Charset charset = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = charset.newDecoder(); CharsetEncoder encoder = charset.newEncoder(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); CharBuffer charBuffer = CharBuffer.allocate(1024); InetSocketAddress socketAddress = new InetSocketAddress(host, 80); channel = SocketChannel.open(); channel.configureBlocking(false); channel.connect(socketAddress); selector = Selector.open(); channel.register(selector, SelectionKey.OP_CONNECT | SelectionKey.OP_READ); while (selector.select(500) > 0) { Set readyKeys = selector.selectedKeys(); try { Iterator readyItor = readyKeys.iterator(); while (readyItor.hasNext()) { SelectionKey key = (SelectionKey) readyItor.next(); readyItor.remove(); SocketChannel keyChannel = (SocketChannel) key.channel(); if (key.isConnectable()) { if (keyChannel.isConnectionPending()) { keyChannel.finishConnect(); } String request = "GET " + file + " \r\n\r\n"; keyChannel.write(encoder.encode(CharBuffer.wrap(request))); } else if (key.isReadable()) { keyChannel.read(buffer); buffer.flip(); decoder.decode(buffer, charBuffer, false); charBuffer.flip(); System.out.print(charBuffer); buffer.clear(); charBuffer.clear(); } else { System.err.println("Unknown key"); } } } catch (ConcurrentModificationException e) { } } } catch (UnknownHostException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } finally { if (channel != null) { try { channel.close(); } catch (IOException ignored) { } } } System.out.println("\nDone."); }
From source file:com.compomics.colims.core.util.AccessionConverter.java
/** * fetches the html page at the passed url as a string * * @param aUrl the url to fetch the html page from * @return the page at the url in textual form * @throws IOException/* w w w . j ava 2 s . co m*/ */ public static String getPage(String aUrl) throws IOException { StringBuilder input = new StringBuilder(); String htmlPage; try (Reader r = openReader(aUrl)) { CharBuffer buffer = CharBuffer.allocate(256); while ((r.read(buffer)) != -1) { input.append(buffer.flip()); buffer.clear(); } } catch (IOException e) { throw new IOException("EMBL-EBI server is not available at the moment, please try again later."); } htmlPage = input.toString(); return htmlPage; }
From source file:fi.johannes.kata.ocr.utils.files.CFileOperations.java
public static void writeToFile(ByteBuffer bb, String outputFile) throws IOException { bb.flip();//w w w .j a v a 2 s . co m try (BufferedWriter bw = new BufferedWriter(new PrintWriter(outputFile, "UTF-8"))) { CharBuffer charBuffer = StandardCharsets.UTF_8.decode(bb); bw.write(charBuffer.toString()); charBuffer.clear(); } }
From source file:fi.johannes.kata.ocr.utils.files.CFileOperations.java
public static void writeChunk(CharBuffer cb, String outputFile) throws IOException { cb.flip();//from w ww . jav a 2s . co m try (BufferedWriter bw = new BufferedWriter(new PrintWriter(outputFile))) { bw.write(cb.toString()); cb.clear(); } }