List of usage examples for java.nio ByteBuffer getInt
public abstract int getInt(int index);
From source file:com.woodcomputing.bobbin.format.JEFFormat.java
@Override public Design load(File file) { byte[] bytes = null; try (InputStream is = new FileInputStream(file)) { bytes = IOUtils.toByteArray(is); } catch (IOException ex) { log.catching(ex);/*from w w w . jav a 2s .c o m*/ } ByteBuffer bb = ByteBuffer.wrap(bytes); bb.order(ByteOrder.LITTLE_ENDIAN); JEF jef = new JEF(); jef.setFirstStitchLocation(bb.getInt(0)); log.debug("First Stitch Location: {}", jef.getFirstStitchLocation()); jef.setThreadChangeCount(bb.getInt(24)); log.debug("Threads Changes: {}", jef.getThreadChangeCount()); jef.setHoop(Hoop.id2Hoop(bb.getInt(32))); log.debug("Hoop: {}", jef.getHoop()); jef.setStitchCount(bb.getInt(28)); log.debug("Stitch Count: {}", jef.getStitchCount()); bb.position(116); JEFColor[] colors = new JEFColor[jef.getThreadChangeCount()]; for (int i = 0; i < jef.getThreadChangeCount(); i++) { colors[i] = jefColorMap.get(bb.getInt()); } jef.setThreadColors(colors); for (int i = 0; i < jef.getThreadChangeCount(); i++) { log.debug("ThreadType{}: {}", i, bb.getInt()); } int dx = 0; int dy = 0; int cx = 0; int cy = 0; int nx = 0; int ny = 0; int change = 1; int stitches = 0; boolean isMove = false; bb.position(jef.getFirstStitchLocation()); JEFColor color = jef.getThreadColors()[change - 1]; Design design = new Design(); StitchGroup stitchGroup = new StitchGroup(); stitchGroup.setColor(color.getRgb()); for (int stitch = 1; stitch < jef.getStitchCount(); stitch++) { dx = bb.get(); dy = bb.get(); if (dx == -128) { switch (dy) { case 1: log.debug("change: {}", bb.position()); change++; color = jef.getThreadColors()[change - 1]; design.getStitchGroups().add(stitchGroup); stitchGroup = new StitchGroup(); stitchGroup.setColor(color.getRgb()); // bb.get(); // bb.get(); continue; case 2: // log.debug("move"); isMove = true; break; case 16: log.debug("last"); isMove = true; break; } } else { nx = cx + dx; ny = cy + dy; if (isMove) { isMove = false; } // } else { // log.debug("stitch"); stitches++; Stitch designStitch = new Stitch(cx, -cy, nx, -ny); stitchGroup.getStitches().add(designStitch); // } cx = nx; cy = ny; } } log.debug("Changes: {} Stitches {} End: {}", change, stitches, bb.position()); return design; }
From source file:FindInt.java
public int seek() throws IOException { int num = -1; try (SeekableByteChannel fc = Files.newByteChannel(file)) { ByteBuffer buf = ByteBuffer.allocate(8); //Get the offset into the file. fc.read(buf);// w w w .j a v a 2s . c om long offset = buf.getLong(0); //Seek to the offset location. fc.position(offset); buf.rewind(); //Read the 'secret' value. fc.read(buf); num = buf.getInt(0); } catch (IOException x) { System.err.println(x); } return num; }
From source file:com.weibo.api.motan.protocol.v2motan.MotanV2Codec.java
/** * decode data//from ww w . jav a2 s .c om * * @return * @throws IOException */ @Override public Object decode(Channel channel, String remoteIp, byte[] data) throws IOException { MotanV2Header header = MotanV2Header.buildHeader(data); Map<String, String> metaMap = new HashMap<String, String>(); ByteBuffer buf = ByteBuffer.wrap(data); int metaSize = buf.getInt(HEADER_SIZE); int index = HEADER_SIZE + 4; if (metaSize > 0) { byte[] meta = new byte[metaSize]; buf.position(index); buf.get(meta); metaMap = decodeMeta(meta); index += metaSize; } int bodySize = buf.getInt(index); index += 4; Object obj = null; if (bodySize > 0) { byte[] body = new byte[bodySize]; buf.position(index); buf.get(body); if (header.isGzip()) { body = ByteUtil.unGzip(body); } //? Serialization serialization = getSerializaiontByNum(header.getSerialize()); obj = new DeserializableObject(serialization, body); } if (header.isRequest()) { if (header.isHeartbeat()) { return DefaultRpcHeartbeatFactory.getDefaultHeartbeatRequest(header.getRequestId()); } else { DefaultRequest request = new DefaultRequest(); request.setRequestId(header.getRequestId()); request.setInterfaceName(metaMap.remove(M2_PATH)); request.setMethodName(metaMap.remove(M2_METHOD)); request.setParamtersDesc(metaMap.remove(M2_METHOD_DESC)); request.setAttachments(metaMap); if (obj != null) { request.setArguments(new Object[] { obj }); } if (metaMap.get(M2_GROUP) != null) { request.setAttachment(URLParamType.group.getName(), metaMap.get(M2_GROUP)); } if (StringUtils.isNotBlank(metaMap.get(M2_VERSION))) { request.setAttachment(URLParamType.version.getName(), metaMap.get(M2_VERSION)); } return request; } } else { if (header.isHeartbeat()) { return DefaultRpcHeartbeatFactory.getDefaultHeartbeatResponse(header.getRequestId()); } DefaultResponse response = new DefaultResponse(); response.setRequestId(header.getRequestId()); response.setProcessTime(MathUtil.parseLong(metaMap.remove(M2_PROCESS_TIME), 0)); response.setAttachments(metaMap); if (header.getStatus() == MotanV2Header.MessageStatus.NORMAL.getStatus()) {//??? response.setValue(obj); } else { String errmsg = metaMap.remove(M2_ERROR); Exception e = ExceptionUtil.fromMessage(errmsg); if (e == null) { e = (Exception) new MotanServiceException("default remote exception. remote errmsg:" + errmsg); } response.setException(e); } return response; } }
From source file:gridool.memcached.gateway.MemcachedProxyHandler.java
@Override public byte[] handleGet(byte[] key) { final ByteBuffer reqPacket = ByteBuffer.allocate(HEADER_LENGTH + key.length); // request header Header header = new Header(MAGIC_BYTE_REQUEST, OPCODE_GET); header.setBodyLength(GET_EXTRA_LENGTH, key.length, 0); header.encode(reqPacket);/*from w w w. j a v a 2 s. c om*/ // request body (key) reqPacket.put(key); reqPacket.flip(); final byte[] value; final SocketAddress sockAddr = getSocket(key); final ByteChannel channel = sockPool.borrowObject(sockAddr); try { // handle request NIOUtils.writeFully(channel, reqPacket); // handle response header ByteBuffer responseHeaderPacket = ByteBuffer.allocate(HEADER_LENGTH); NIOUtils.readFully(channel, responseHeaderPacket); responseHeaderPacket.flip(); // handle response body int totalBody = responseHeaderPacket.getInt(8); int keyLen = responseHeaderPacket.getShort(2); int extraLen = responseHeaderPacket.get(4); int bodyPos = extraLen + keyLen; int bodyLen = totalBody - bodyPos; if (bodyLen <= 0) { return null; } ByteBuffer responseBodyPacket = ByteBuffer.allocate(totalBody); NIOUtils.readFully(channel, responseBodyPacket); responseBodyPacket.flip(); value = new byte[bodyLen]; responseBodyPacket.get(value, 0, bodyLen); } catch (IOException e) { LOG.error(e); return null; } finally { sockPool.returnObject(sockAddr, channel); } return value; }
From source file:com.woodcomputing.bobbin.format.PESFormat.java
@Override public Design load(File file) { byte[] bytes = null; try (InputStream is = new FileInputStream(file)) { bytes = IOUtils.toByteArray(is); } catch (IOException ex) { log.catching(ex);//from w w w . j ava2s . co m } ByteBuffer bb = ByteBuffer.wrap(bytes); bb.order(ByteOrder.LITTLE_ENDIAN); Design design = new Design(); log.debug("Magic: {}{}{}{}", (char) bb.get(), (char) bb.get(), (char) bb.get(), (char) bb.get()); int pecStart = bb.getInt(8); log.debug("PEC Start: {}", pecStart); byte colorCount = bb.get(pecStart + 48); log.debug("Color Count: {}", colorCount); int colors[] = new int[colorCount]; for (int i = 0; i < colorCount; i++) { colors[i] = bb.get() & 0xFF; log.debug("Color[{}] = {}", i, colors[i]); } bb.position(pecStart + 532); int x; int y; int colorChanges = 0; PESColor color = pesColorMap.get(colors[colorChanges++]); StitchGroup stitchGroup = new StitchGroup(); stitchGroup.setColor(color.getColor()); while (true) { x = bb.get() & 0xFF; y = bb.get() & 0xFF; if (x == 0xFF && y == 0x00) { log.debug("End of stitches"); break; } if (x == 0xFE && y == 0xB0) { int colorIndex = bb.get() & 0xFF; log.debug("Color change: {}", colorIndex); color = pesColorMap.get(colors[colorChanges++]); stitchGroup = new StitchGroup(); stitchGroup.setColor(color.getColor()); continue; } if ((x & 0x80) > 0) { log.debug("Testing X: {} - X & 0x80: {}", x, x & 0x80); if ((x & 0x20) > 0) { log.debug("Stich type TRIM"); } if ((x & 0x10) > 0) { log.debug("Stich type JUMP"); } x = ((x & 0x0F) << 8) + y; if ((x & 0x800) > 0) { x -= 0x1000; } y = bb.get() & 0xFF; } else if (x >= 0x40) { x -= 0x80; } if ((y & 0x80) > 0) { log.debug("Testing Y: {} - Y & 0x80: {}", y, y & 0x80); if ((y & 0x20) > 0) { log.debug("Stich type TRIM"); } if ((y & 0x10) > 0) { log.debug("Stich type JUMP"); } y = ((y & 0x0F) << 8) + bb.get() & 0xFF; if ((y & 0x800) > 0) { y -= 0x1000; } } else if (y >= 0x40) { y -= 0x80; } // Stitch designStitch = new Stitch(cx, -cy, nx, -ny); // stitchGroup.getStitches().add(designStitch); log.debug("X: {} Y: {}", x, y); } log.debug("Color Changes: {}", colorChanges); return design; }
From source file:org.getspout.spoutapi.packet.PacketEntityInformation.java
@Override public void run(int playerId) { SpoutPlayer player = SpoutManager.getPlayerFromId(playerId); if (player != null) { ByteBuffer rawData = ByteBuffer.allocate(data.length); rawData.put(data);/*from w w w .j a v a2s . com*/ ArrayList<LivingEntity> entities = new ArrayList<LivingEntity>(data.length / 4 + 1); for (int i = 0; i < data.length / 4; i++) { int index = i * 4; int id = rawData.getInt(index); Entity entity = SpoutManager.getEntityFromId(id); if (entity != null && entity instanceof LivingEntity) { entities.add((LivingEntity) entity); } } if (entities.size() > 0) { player.sendPacket(new PacketEntityInformation(entities)); player.updateEntitySkins(entities); } } }
From source file:org.spoutcraft.client.packet.PacketEntityInformation.java
public void run(int playerId) { if (Minecraft.theMinecraft.theWorld instanceof WorldClient) { ByteBuffer rawData = ByteBuffer.allocate(data.length); rawData.put(data);//from w w w.j a v a 2 s.co m for (int i = 0; i < data.length / 20; i++) { int index = i * 20; long lsb = rawData.getLong(index); long msb = rawData.getLong(index + 8); int id = rawData.getInt(index + 16); net.minecraft.src.Entity e = SpoutClient.getInstance().getEntityFromId(id); if (e != null) { e.uniqueId = new UUID(msb, lsb); } } } }
From source file:au.org.ala.delta.translation.dist.DistItemsFileWriter.java
private void encodeUnorderedMultistateAttribute(ByteBuffer work, int wordOffset, int bitOffset, IdentificationKeyCharacter keyChar, MultiStateAttribute attribute) { List<Integer> states = keyChar.getPresentStates(attribute); int word = work.getInt(wordOffset * 4); for (int state : states) { int bit = bitOffset + state - 1; if (bit > 31) { work.putInt(wordOffset * 4, word); wordOffset++;//from www . j av a 2 s . co m word = work.getInt(wordOffset * 4); bitOffset -= 32; bit = bitOffset + state - 1; } word |= (1 << bit); } work.putInt(wordOffset * 4, word); }
From source file:com.google.flatbuffers.Table.java
/** * Create a Java `String` from UTF-8 data stored inside the FlatBuffer. * * This allocates a new string and converts to wide chars upon each access, * which is not very efficient. Instead, each FlatBuffer string also comes with an * accessor based on __vector_as_bytebuffer below, which is much more efficient, * assuming your Java program can handle UTF-8 data directly. * * @param offset An `int` index into the Table's ByteBuffer. * @return Returns a `String` from the data stored inside the FlatBuffer at `offset`. *///from www.j a v a 2 s. c o m protected String __string(int offset) { CharsetDecoder decoder = UTF8_DECODER.get(); decoder.reset(); offset += bb.getInt(offset); ByteBuffer src = bb.duplicate().order(ByteOrder.LITTLE_ENDIAN); int length = src.getInt(offset); src.position(offset + SIZEOF_INT); src.limit(offset + SIZEOF_INT + length); int required = (int) ((float) length * decoder.maxCharsPerByte()); CharBuffer dst = CHAR_BUFFER.get(); if (dst == null || dst.capacity() < required) { dst = CharBuffer.allocate(required); CHAR_BUFFER.set(dst); } dst.clear(); try { CoderResult cr = decoder.decode(src, dst, true); if (!cr.isUnderflow()) { cr.throwException(); } } catch (CharacterCodingException x) { throw new Error(x); } return dst.flip().toString(); }
From source file:com.codestation.henkakuserver.HenkakuServer.java
/** * Finalize the exploit with the addesses from the device * * @param exploit payload compiled code//from w w w . jav a2s . c om * @param params list of addresses from the device * @return patched shellcode * @throws Exception */ private byte[] patchExploit(byte[] exploit, Map<String, String> params) throws Exception { if (params.size() != 7) { throw new Exception("invalid argument count"); } ArrayList<Long> args = new ArrayList<>(); args.add(0L); for (int i = 1; i <= 7; ++i) { String arg = String.format("a%s", i); if (params.containsKey(arg)) { args.add(Long.parseLong(params.get(arg), 16)); } else { throw new Exception(String.format("argument %s is missing", arg)); } } byte[] copy = new byte[exploit.length]; System.arraycopy(exploit, 0, copy, 0, exploit.length); ByteBuffer buf = ByteBuffer.wrap(copy).order(ByteOrder.LITTLE_ENDIAN); int size_words = buf.getInt(0); int dsize = buf.getInt(4 + 0x10); int csize = buf.getInt(4 + 0x20); long data_base = args.get(1) + csize; for (int i = 1; i < size_words; ++i) { long add = 0; byte x = buf.get(size_words * 4 + 4 + i - 1); if (x == 1) { add = data_base; } else if (x != 0) { add = args.get(x); } buf.putInt(i * 4, buf.getInt(i * 4) + (int) add); } byte[] out = new byte[dsize + csize]; System.arraycopy(copy, 4 + 0x40, out, csize, dsize); System.arraycopy(copy, 4 + 0x40 + dsize, out, 0, csize); return out; }