@Override public boolean matches(String messageAddress) { final List<String> messageAddressParts = splitIntoParts(messageAddress); return matches(patternParts, 0, messageAddressParts, 0); }
/** * Write bytes into the byte stream. * @param bytes bytes to be written */ public void write(byte[] bytes) { writeInteger32ToByteArray(bytes.length); writeUnderHandler(bytes); alignStream(); }
public void dispatchPacket(final OSCPacket packet, final Date timestamp) { if (packet instanceof OSCBundle) { dispatchBundle((OSCBundle) packet); } else { dispatchMessage((OSCMessage) packet, timestamp); } }
/** * Reads a string from the byte stream. * @return the next string in the byte stream */ private String readString(final Input rawInput) { final int strLen = lengthOfCurrentString(rawInput); final String res = new String(rawInput.getBytes(), rawInput.getStreamPosition(), strLen, charset); rawInput.addToStreamPosition(strLen); moveToFourByteBoundry(rawInput); return res; }
/** * Reads a binary blob from the byte stream. * @return the next blob in the byte stream */ private byte[] readBlob(final Input rawInput) { final int blobLen = readInteger(rawInput); final byte[] res = new byte[blobLen]; System.arraycopy(rawInput.getBytes(), rawInput.getStreamPosition(), res, 0, blobLen); rawInput.addToStreamPosition(blobLen); moveToFourByteBoundry(rawInput); return res; }
/** * Create an OSCPort that listens using a specified socket. * @param socket DatagramSocket to listen on. */ public OSCPortIn(final DatagramSocket socket) { super(socket, socket.getLocalPort()); this.converter = new OSCByteArrayToJavaConverter(); this.dispatcher = new OSCPacketDispatcher(); this.listeningThread = null; }
/** * Convert the arguments into a byte array. * Used internally only. * @param stream where to write the arguments to */ private void computeArgumentsByteArray(final OSCJavaToByteArrayConverter stream) { stream.write(','); stream.writeTypes(arguments); for (final Object argument : arguments) { stream.write(argument); } }
/** * Get the length of the string currently in the byte stream. */ private int lengthOfCurrentString(final Input rawInput) { int len = 0; while (rawInput.getBytes()[rawInput.getStreamPosition() + len] != 0) { len++; } return len; }
/** * Move to the next byte with an index in the byte array * which is dividable by four. */ private void moveToFourByteBoundry(final Input rawInput) { // If i am already at a 4 byte boundry, I need to move to the next one final int mod = rawInput.getStreamPosition() % 4; rawInput.addToStreamPosition(4 - mod); } }
/** * Registers a listener that will be notified of incoming messages, * if their address matches the given selector. * @param addressSelector a custom address selector * @param listener will be notified of incoming packets, if they match */ public void addListener(final AddressSelector addressSelector, final OSCListener listener) { dispatcher.addListener(addressSelector, listener); } }
/** * Reads a char from the byte stream. * @return a {@link Character} */ private Character readChar(final Input rawInput) { return (char) rawInput.getBytes()[rawInput.getAndIncreaseStreamPositionByOne()]; }
public void dispatchPacket(final OSCPacket packet) { dispatchPacket(packet, null); }
/** * Write an integer into the byte stream. * @param anInt the integer to be written */ public void write(int anInt) { writeInteger32ToByteArray(anInt); }
/** * @param aLong the double precision integer to be written */ public void write(Long aLong) { writeInteger64ToByteArray(aLong); }
/** * Convert the address into a byte array. * Used internally only. * @param stream where to write the address to */ private void computeAddressByteArray(final OSCJavaToByteArrayConverter stream) { stream.write(address); }
/** * Create an OSCPort that listens on the specified port, * and decodes strings with a specific character set. * @param port UDP port to listen on. * @param charset how to decode strings read from incoming packages. * This includes message addresses and string parameters. * @throws SocketException if the port number is invalid, * or there is already a socket listening on it */ public OSCPortIn(final int port, final Charset charset) throws SocketException { this(port); this.converter.setCharset(charset); }
/** * Write a 32 bit integer to the byte array without allocating memory. * @param value a 32 bit integer. */ private void writeInteger32ToByteArray(int value) { //byte[] intBytes = new byte[4]; //I allocated the this buffer globally so the GC has less work intBytes[3] = (byte)value; value >>>= 8; intBytes[2] = (byte)value; value >>>= 8; intBytes[1] = (byte)value; value >>>= 8; intBytes[0] = (byte)value; writeUnderHandler(intBytes); }
public OSCPatternAddressSelector(String selector) { this.patternParts = splitIntoParts(selector); }
/** * Checks whether my byte array is a bundle. * From the OSC 1.0 specifications: * <quote> * The contents of an OSC packet must be either an OSC Message * or an OSC Bundle. The first byte of the packet's contents unambiguously * distinguishes between these two alternatives. * </quote> * @return true if it the byte array is a bundle, false o.w. */ private boolean isBundle(final Input rawInput) { // The shortest valid packet may be no shorter then 4 bytes, // thus we may assume to always have a byte at index 0. return rawInput.getBytes()[0] == BUNDLE_IDENTIFIER; }
/** * @param anInt the integer to be written */ public void write(Integer anInt) { writeInteger32ToByteArray(anInt); }