List of usage examples for java.nio ByteBuffer clear
public final Buffer clear()
From source file:org.mhisoft.common.util.FileUtils.java
/** * @param source/* w ww . ja v a2 s .c om*/ * @param target * @throws IOException */ public static void copyFile(final File source, final File target) throws IOException { FileChannel in = null; FileChannel out = null; // long totalFileSize = 0; try { in = new FileInputStream(source).getChannel(); out = new FileOutputStream(target).getChannel(); //totalFileSize = in.size(); ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER); int readSize = in.read(buffer); long totalRead = 0; //int progress = 0; //long startTime, endTime ; while (readSize != -1) { //startTime = System.currentTimeMillis(); totalRead = totalRead + readSize; //progress = (int) (totalRead * 100 / totalFileSize); buffer.flip(); while (buffer.hasRemaining()) { out.write(buffer); //System.out.printf("."); //showPercent(rdProUI, totalSize/size ); } buffer.clear(); readSize = in.read(buffer); //endTime = System.currentTimeMillis(); } } finally { close(in); close(out); } }
From source file:org.sglover.nlp.EntityExtracter.java
private String getContent(ReadableByteChannel channel) throws IOException { StringBuilder sb = new StringBuilder(); ByteBuffer bb = ByteBuffer.allocate(2048); int c = -1;/* www . j a va 2 s.co m*/ do { c = channel.read(bb); bb.flip(); bb.clear(); sb.append(new String(bb.array(), "UTF-8")); } while (c != -1); String content = sb.toString(); return content; }
From source file:org.apache.hadoop.io.crypto.aes.NativeOpensslAESCipher.java
/** * input and output are direct buffer for speed improvement. * /*from ww w . j av a 2 s. c o m*/ * @param input * @param inputLength * @param output * @return the output length * @throws Exception when error happens */ @Override public int doFinal(ByteBuffer input, int inputLength, ByteBuffer output) throws Exception { if (!inited) { throw new Exception("Cipher not inited, please call init()"); } output.clear(); output.limit(0); int length = doFinal(context, input, inputLength, output); if (0 == length) { throw new Exception("Decompressed length is 0!!!"); } output.limit(length); return length; }
From source file:org.apache.nifi.processors.standard.util.BaseStrictSyslog5424ParserTest.java
@Test public void testParseWithSender() { final String sender = "127.0.0.1"; final String message = "<14>1 2014-06-20T09:14:07+00:00 loggregator" + " d0602076-b14a-4c55-852a-981e7afeed38 DEA MSG-01" + " [exampleSDID@32473 iut=\"3\" eventSource=\"Application\" eventID=\"1011\"]" + " [exampleSDID@32480 iut=\"4\" eventSource=\"Other Application\" eventID=\"2022\"] Removing instance"; final byte[] bytes = message.getBytes(CHARSET); final ByteBuffer buffer = ByteBuffer.allocate(bytes.length); buffer.clear(); buffer.put(bytes);/* ww w . j av a 2 s .c o m*/ final Syslog5424Event event = parser.parseEvent(buffer, sender); Assert.assertNotNull(event); Assert.assertTrue(event.isValid()); Assert.assertEquals(sender, event.getSender()); }
From source file:org.lispmob.noroot.IPC.java
public void run() { int len = 0;/* w ww .ja v a 2 s . c o m*/ ByteBuffer buf = ByteBuffer.allocate(9000); while (!ipc_thread.isInterrupted()) { buf.clear(); try { len = ipc_channel.read(buf); if (len == 0) { continue; } buf.flip(); String json_str = EncodingUtils.getString(buf.array(), "utf8"); JSONObject jObj = new JSONObject(json_str); int ipc_type = jObj.getInt("type"); System.out.println("LISPmob: Received IPC message: " + ipc_type); switch (ipc_type) { case IPC_LOG_MSG: LISPmobVPNService.err_msg_code = jObj.getInt("err_msg_code"); Thread.sleep(1000); if (LISPmobVPNService.err_msg_code != 0) { /* If LISPmob is not the active windows, the error msg code is not clean * and we send a notification of the error */ Resources res = vpn_service.getResources(); String[] err_msg = res.getStringArray(R.array.ErrMsgArray); String msg = err_msg[LISPmobVPNService.err_msg_code]; //notifications.notify_msg( msg); } break; case IPC_PROTECT_SOCKS: int socket = jObj.getInt("socket"); if (socket != -1) { boolean sock_protect = false; int retry = 0; while (!sock_protect && retry < 30) { if (!vpn_service.protect(socket)) { retry++; Thread.sleep(200); } else { sock_protect = true; System.out.println( "LISPmob: The socket " + socket + " has been protected (VPN Service)"); } } } break; default: System.out.println("***** Unknown IPC message: " + ipc_type); break; } } catch (Exception e) { e.printStackTrace(); } } }
From source file:MainClass.java
public void run() { ByteBuffer sizeb = ByteBuffer.allocate(4); try {//from w w w . j a v a2s . c o m while (sizeb.hasRemaining()) in.read(sizeb); sizeb.flip(); int howMany = sizeb.getInt(); sizeb.clear(); for (int i = 0; i < howMany; i++) { while (sizeb.hasRemaining()) in.read(sizeb); sizeb.flip(); int length = sizeb.getInt(); sizeb.clear(); ByteBuffer data = ByteBuffer.allocate(length); while (data.hasRemaining()) in.read(data); BigInteger result = new BigInteger(data.array()); System.out.println(result); } } catch (IOException ex) { System.err.println(ex); } finally { try { in.close(); } catch (Exception ex) { // We tried } } }
From source file:org.apache.nifi.processors.standard.util.BaseStrictSyslog5424ParserTest.java
@Test public void testRFC5424WithoutVersion() { final String pri = "34"; final String version = "-"; final String stamp = "2003-10-11T22:14:15.003Z"; final String host = "mymachine.example.com"; final String appName = "su"; final String procId = "-"; final String msgId = "ID17"; final String structuredData = "-"; final String body = "BOM'su root' failed for lonvick on /dev/pts/8"; final String message = "<" + pri + ">" + version + " " + stamp + " " + host + " " + appName + " " + procId + " " + msgId + " " + "-" + " " + body; final byte[] bytes = message.getBytes(CHARSET); final ByteBuffer buffer = ByteBuffer.allocate(bytes.length); buffer.clear(); buffer.put(bytes);// w w w.j a va 2 s . co m final Syslog5424Event event = parser.parseEvent(buffer); Assert.assertFalse(event.isValid()); }
From source file:org.apache.nifi.processors.standard.util.BaseStrictSyslog5424ParserTest.java
@Test public void testVariety() { final List<String> messages = new ArrayList<>(); // supported examples from RFC 5424 messages.add("<34>1 2003-10-11T22:14:15.003Z mymachine.example.com su - " + "ID47 - BOM'su root' failed for lonvick on /dev/pts/8"); messages.add("<165>1 2003-08-24T05:14:15.000003-07:00 192.0.2.1 myproc " + "8710 - - %% It's time to make the do-nuts."); messages.add("<14>1 2014-06-20T09:14:07+00:00 loggregator" + " d0602076-b14a-4c55-852a-981e7afeed38 DEA MSG-01" + " [exampleSDID@32473 iut=\"3\" eventSource=\"Application\" eventID=\"1011\"]" + " [exampleSDID@32480 iut=\"4\" eventSource=\"Other Application\" eventID=\"2022\"] Removing instance"); for (final String message : messages) { final byte[] bytes = message.getBytes(CHARSET); final ByteBuffer buffer = ByteBuffer.allocate(bytes.length); buffer.clear(); buffer.put(bytes);//from w w w . j a v a 2 s . c o m final Syslog5424Event event = parser.parseEvent(buffer); Assert.assertTrue(event.isValid()); } }
From source file:ti.modules.titanium.utils.UtilsModule.java
public String transcodeString(String orig, String inEncoding, String outEncoding) { try {/*from w ww .j a va 2s . c o m*/ Charset charsetOut = Charset.forName(outEncoding); Charset charsetIn = Charset.forName(inEncoding); ByteBuffer bufferIn = ByteBuffer.wrap(orig.getBytes(charsetIn.name())); CharBuffer dataIn = charsetIn.decode(bufferIn); bufferIn.clear(); bufferIn = null; ByteBuffer bufferOut = charsetOut.encode(dataIn); dataIn.clear(); dataIn = null; byte[] dataOut = bufferOut.array(); bufferOut.clear(); bufferOut = null; return new String(dataOut, charsetOut.name()); } catch (UnsupportedEncodingException e) { Log.e(TAG, "Unsupported encoding: " + e.getMessage(), e); } return null; }
From source file:org.apache.hadoop.mapred.nativetask.buffer.DirectBufferPool.java
public void returnBuffer(ByteBuffer buffer) throws IOException { if (null == buffer || !buffer.isDirect()) { throw new IOException("the buffer is null or the buffer returned is not direct buffer"); }/*from ww w .j a va 2s . co m*/ buffer.clear(); int capacity = buffer.capacity(); Queue<WeakReference<ByteBuffer>> list = bufferMap.get(capacity); if (null == list) { list = new ConcurrentLinkedQueue<WeakReference<ByteBuffer>>(); Queue<WeakReference<ByteBuffer>> prev = bufferMap.putIfAbsent(capacity, list); if (prev != null) { list = prev; } } list.add(new WeakReference<ByteBuffer>(buffer)); }