fork(1) download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.io.*;
  5. import java.nio.*;
  6. import java.nio.channels.*;
  7.  
  8. class Ideone {
  9. public static void main (String[] args) throws java.lang.Exception {
  10. byte[] fileImage = {0x61, 0x62, 0x63, 0x0, 0x0, 0x5E, (byte)0xD0, (byte)0xB2};
  11.  
  12. try(InputStream fis = new ByteArrayInputStream(fileImage)){
  13. ReadableByteChannel ch = Channels.newChannel(fis);
  14. ByteBuffer buf = ByteBuffer.allocate(64*1024);
  15. ch.read(buf);
  16. buf.position(0);
  17.  
  18. // max 4, null terminated, UTF-8
  19. String a;
  20. {
  21. byte[] bufS = new byte[4];
  22. buf.get(bufS);
  23. a = new String(bufS, 0, getTerm(bufS), "UTF-8");
  24. }
  25.  
  26. // little endian, unsigned int32
  27. buf.order(ByteOrder.LITTLE_ENDIAN);
  28. long b = buf.getInt() & 0xFFFFFFFFL;
  29.  
  30. System.out.println("a["+a+"] a.len["+a.length()+"] b["+b+"]");
  31. }
  32. }
  33.  
  34. private static int getTerm(byte[] b){
  35. for(int i=0,sz=b.length; i<sz; i++){
  36. if(b[i] == 0){return i;}
  37. }
  38. return b.length;
  39. }
  40. }
  41.  
Success #stdin #stdout 0.11s 320576KB
stdin
Standard input is empty
stdout
a[abc] a.len[3] b[3000000000]