List of usage examples for java.nio ByteBuffer get
public abstract byte get();
From source file:BufferConverter.java
public static void main(String[] arguments) { try {/*from w w w . j ava2 s . com*/ String data = "friends.dat"; FileInputStream inData = new FileInputStream(data); FileChannel inChannel = inData.getChannel(); long inSize = inChannel.size(); ByteBuffer source = ByteBuffer.allocate((int) inSize); inChannel.read(source, 0); source.position(0); for (int i = 0; source.remaining() > 0; i++) System.out.print(source.get() + " "); source.position(0); Charset ascii = Charset.forName("US-ASCII"); CharsetDecoder toAscii = ascii.newDecoder(); CharBuffer destination = toAscii.decode(source); destination.position(0); System.out.println("\n\nNew character data:"); for (int i = 0; destination.remaining() > 0; i++) System.out.print(destination.get()); } catch (Exception ioe) { System.out.println(ioe.getMessage()); } }
From source file:Main.java
public static void main(String[] args) { ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, 'a' }); bb.rewind();/* w w w .j av a2s . c o m*/ System.out.println("Byte Buffer"); while (bb.hasRemaining()) System.out.println(bb.position() + " -> " + bb.get()); CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer(); System.out.println("Char Buffer"); while (cb.hasRemaining()) System.out.println(cb.position() + " -> " + cb.get()); FloatBuffer fb = ((ByteBuffer) bb.rewind()).asFloatBuffer(); System.out.println("Float Buffer"); while (fb.hasRemaining()) System.out.println(fb.position() + " -> " + fb.get()); IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer(); System.out.println("Int Buffer"); while (ib.hasRemaining()) System.out.println(ib.position() + " -> " + ib.get()); LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer(); System.out.println("Long Buffer"); while (lb.hasRemaining()) System.out.println(lb.position() + " -> " + lb.get()); ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer(); System.out.println("Short Buffer"); while (sb.hasRemaining()) System.out.println(sb.position() + " -> " + sb.get()); DoubleBuffer db = ((ByteBuffer) bb.rewind()).asDoubleBuffer(); System.out.println("Double Buffer"); while (db.hasRemaining()) System.out.println(db.position() + " -> " + db.get()); }
From source file:Main.java
public static void main(String[] argv) throws Exception { ReadableByteChannel channel = new FileInputStream("infile.dat").getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(10); int numRead = 0; while (numRead >= 0) { buf.rewind();/*from w w w.j a v a2s.c o m*/ numRead = channel.read(buf); buf.rewind(); for (int i = 0; i < numRead; i++) { byte b = buf.get(); } } }
From source file:Main.java
public static void main(String[] argv) throws Exception { ReadableByteChannel channel = new FileInputStream("infile").getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(10); int numRead = 0; while (numRead >= 0) { buf.rewind();// w w w.ja va2 s . c om numRead = channel.read(buf); buf.rewind(); for (int i = 0; i < numRead; i++) { byte b = buf.get(); } } }
From source file:Main.java
public static void main(String[] argv) throws Exception { ReadableByteChannel channel = new FileInputStream("infile").getChannel(); ByteBuffer buf = ByteBuffer.allocateDirect(10); int numRead = 0; while (numRead >= 0) { buf.rewind();// w w w . j a va2 s . c o m numRead = channel.read(buf); buf.rewind(); // Read bytes from ByteBuffer; see also for (int i = 0; i < numRead; i++) { byte b = buf.get(); } } }
From source file:ExplicitChannelRead.java
public static void main(String args[]) { FileInputStream fIn;/* w w w . j a v a2s . co m*/ FileChannel fChan; long fSize; ByteBuffer mBuf; try { fIn = new FileInputStream("test.txt"); fChan = fIn.getChannel(); fSize = fChan.size(); mBuf = ByteBuffer.allocate((int) fSize); fChan.read(mBuf); mBuf.rewind(); for (int i = 0; i < fSize; i++) System.out.print((char) mBuf.get()); fChan.close(); fIn.close(); } catch (IOException exc) { System.out.println(exc); System.exit(1); } }
From source file:MainClass.java
public static void main(String args[]) { FileInputStream fIn;/* w w w . jav a2s. c om*/ FileChannel fChan; long fSize; ByteBuffer mBuf; try { fIn = new FileInputStream("test.txt"); fChan = fIn.getChannel(); fSize = fChan.size(); mBuf = ByteBuffer.allocate((int) fSize); fChan.read(mBuf); mBuf.rewind(); for (int i = 0; i < fSize; i++) System.out.print((char) mBuf.get()); fChan.close(); fIn.close(); } catch (IOException exc) { System.out.println(exc); } }
From source file:edu.hawaii.soest.kilonalu.dvp2.DavisWxParser.java
public static void main(String[] args) { // Ensure we have a path to the binary file if (args.length != 1) { logger.info("Please provide the path to a file containing a binary LOOP packet."); System.exit(1);/* ww w .j a v a2 s . c o m*/ } else { try { // open and read the file File packetFile = new File(args[0]); FileInputStream fis = new FileInputStream(packetFile); FileChannel fileChannel = fis.getChannel(); ByteBuffer inBuffer = ByteBuffer.allocateDirect(8192); ByteBuffer packetBuffer = ByteBuffer.allocateDirect(8192); while (fileChannel.read(inBuffer) != -1 || inBuffer.position() > 0) { inBuffer.flip(); packetBuffer.put(inBuffer.get()); inBuffer.compact(); } fileChannel.close(); fis.close(); packetBuffer.put(inBuffer.get()); // create an instance of the parser, and report the field contents after parsing DavisWxParser davisWxParser = new DavisWxParser(packetBuffer); // Set up a simple logger that logs to the console PropertyConfigurator.configure(davisWxParser.getLogConfigurationFile()); logger.info("loopID: " + davisWxParser.getLoopID()); logger.info("barTrend: " + davisWxParser.getBarTrend()); logger.info("barTrendAsString: " + davisWxParser.getBarTrendAsString()); logger.info("packetType: " + davisWxParser.getPacketType()); logger.info("nextRecord: " + davisWxParser.getNextRecord()); logger.info("barometer: " + davisWxParser.getBarometer()); logger.info("insideTemperature: " + davisWxParser.getInsideTemperature()); logger.info("insideHumidity: " + davisWxParser.getInsideHumidity()); logger.info("outsideTemperature: " + davisWxParser.getOutsideTemperature()); logger.info("windSpeed: " + davisWxParser.getWindSpeed()); logger.info("tenMinuteAverageWindSpeed: " + davisWxParser.getTenMinuteAverageWindSpeed()); logger.info("windDirection: " + davisWxParser.getWindDirection()); logger.info( "extraTemperatures: " + Arrays.toString(davisWxParser.getExtraTemperatures())); logger.info( "soilTemperatures: " + Arrays.toString(davisWxParser.getSoilTemperatures())); logger.info( "leafTemperatures: " + Arrays.toString(davisWxParser.getLeafTemperatures())); logger.info("outsideHumidity: " + davisWxParser.getOutsideHumidity()); logger.info( "extraHumidities: " + Arrays.toString(davisWxParser.getExtraHumidities())); logger.info("rainRate: " + davisWxParser.getRainRate()); logger.info("uvRadiation: " + davisWxParser.getUvRadiation()); logger.info("solarRadiation: " + davisWxParser.getSolarRadiation()); logger.info("stormRain: " + davisWxParser.getStormRain()); logger.info("currentStormStartDate: " + davisWxParser.getCurrentStormStartDate()); logger.info("dailyRain: " + davisWxParser.getDailyRain()); logger.info("monthlyRain: " + davisWxParser.getMonthlyRain()); logger.info("yearlyRain: " + davisWxParser.getYearlyRain()); logger.info("dailyEvapoTranspiration: " + davisWxParser.getDailyEvapoTranspiration()); logger.info("monthlyEvapoTranspiration: " + davisWxParser.getMonthlyEvapoTranspiration()); logger.info("yearlyEvapoTranspiration: " + davisWxParser.getYearlyEvapoTranspiration()); logger.info("soilMoistures: " + Arrays.toString(davisWxParser.getSoilMoistures())); logger.info("leafWetnesses: " + Arrays.toString(davisWxParser.getLeafWetnesses())); logger.info("insideAlarm: " + davisWxParser.getInsideAlarm()); logger.info("rainAlarm: " + davisWxParser.getRainAlarm()); logger.info("outsideAlarms: " + davisWxParser.getOutsideAlarms()); logger.info("extraTemperatureHumidityAlarms: " + davisWxParser.getExtraTemperatureHumidityAlarms()); logger.info("soilLeafAlarms: " + davisWxParser.getSoilLeafAlarms()); logger.info("transmitterBatteryStatus: " + davisWxParser.getTransmitterBatteryStatus()); logger.info("consoleBatteryVoltage: " + davisWxParser.getConsoleBatteryVoltage()); logger.info("forecastIconValues: " + davisWxParser.getForecastAsString()); logger.info("forecastRuleNumber: " + davisWxParser.getForecastRuleNumberAsString()); logger.info("timeOfSunrise: " + davisWxParser.getTimeOfSunrise()); logger.info("timeOfSunset: " + davisWxParser.getTimeOfSunset()); logger.info("recordDelimiter: " + davisWxParser.getRecordDelimiterAsHexString()); logger.info("crcChecksum: " + davisWxParser.getCrcChecksum()); } catch (java.io.FileNotFoundException fnfe) { fnfe.printStackTrace(); } catch (java.io.IOException ioe) { ioe.printStackTrace(); } } }
From source file:Main.java
public static int getNalType(final ByteBuffer buffer) { return buffer.get() & 0x1f; }
From source file:Main.java
public static short getUnsignedByte(ByteBuffer bb) { return ((short) (bb.get() & 0xff)); }