front_processing/extern/TUIO_JAVA/src/com/illposed/osc/OSCBundle.java
changeset 0 6fefd4afe506
child 9 0f44b7360c8d
equal deleted inserted replaced
-1:000000000000 0:6fefd4afe506
       
     1 
       
     2 /**
       
     3  * @author cramakrishnan
       
     4  *
       
     5  * Copyright (C) 2003, C. Ramakrishnan / Illposed Software
       
     6  * All rights reserved.
       
     7  * 
       
     8  * See license.txt (or license.rtf) for license information.
       
     9  * 
       
    10  *
       
    11  * OscBundle represents a collection of OscPackets.
       
    12  *
       
    13  * Use this when you want to send a bunch of OscPackets
       
    14  * in one go.
       
    15  *
       
    16  * Internally, I use Vector to maintain jdk1.1 compatability
       
    17  */
       
    18 
       
    19 package com.illposed.osc;
       
    20 import java.math.BigInteger;
       
    21 import java.util.Date;
       
    22 import java.util.Enumeration;
       
    23 import java.util.GregorianCalendar;
       
    24 import java.util.Vector;
       
    25 
       
    26 import com.illposed.osc.utility.*;
       
    27 
       
    28 public class OSCBundle extends OSCPacket {
       
    29 
       
    30 	protected Date timestamp;
       
    31 	//	protected OSCPacket[] packets;
       
    32 	protected Vector<OSCPacket> packets;
       
    33 	public static final BigInteger SECONDS_FROM_1900_to_1970 =
       
    34 		new BigInteger("2208988800");
       
    35 	// 17 leap years
       
    36 
       
    37 	/**
       
    38 	 * Create a new OSCBundle, with a timestamp of now.
       
    39 	 * You can add packets to the bundle with addPacket()
       
    40 	 */
       
    41 	public OSCBundle() {
       
    42 		this(null, GregorianCalendar.getInstance().getTime());
       
    43 	}
       
    44 	
       
    45 	/**
       
    46 	 * Create an OSCBundle with the specified timestamp
       
    47 	 * @param timestamp
       
    48 	 */
       
    49 	public OSCBundle(Date timestamp) {
       
    50 		this(null, timestamp);
       
    51 	}
       
    52 
       
    53 	/**
       
    54 	 * @param newPackets Array of OSCPackets to initialize this object with
       
    55 	 */
       
    56 	public OSCBundle(OSCPacket[] newPackets) {
       
    57 		this(newPackets, GregorianCalendar.getInstance().getTime());
       
    58 	}
       
    59 
       
    60 	/**
       
    61 	 * @param newPackets OscPacket[]
       
    62 	 * @param time java.lang.Time
       
    63 	 */
       
    64 	public OSCBundle(OSCPacket[] newPackets, Date newTimestamp) {
       
    65 		super();
       
    66 		if (null != newPackets) {
       
    67 			packets = new Vector<OSCPacket>(newPackets.length);
       
    68 			for (int i = 0; i < newPackets.length; i++) {
       
    69 				packets.add(newPackets[i]);
       
    70 			}
       
    71 		} else
       
    72 			packets = new Vector<OSCPacket>();
       
    73 		timestamp = newTimestamp;
       
    74 		init();
       
    75 	}
       
    76 	
       
    77 	/**
       
    78 	 * Return the timestamp for this bundle
       
    79 	 * @return a Date
       
    80 	 */
       
    81 	public Date getTimestamp() {
       
    82 		return timestamp;
       
    83 	}
       
    84 	
       
    85 	/**
       
    86 	 * Set the timestamp for this bundle
       
    87 	 * @param timestamp
       
    88 	 */
       
    89 	public void setTimestamp(Date timestamp) {
       
    90 		this.timestamp = timestamp;
       
    91 	}
       
    92 	
       
    93 	/**
       
    94 	 * Add a packet to the list of packets in this bundle
       
    95 	 * @param packet
       
    96 	 */
       
    97 	public void addPacket(OSCPacket packet) {
       
    98 		packets.add(packet);
       
    99 	}
       
   100 	
       
   101 	/**
       
   102 	 * Get the packets contained in this bundle
       
   103 	 * @return an array of packets
       
   104 	 */
       
   105 	public OSCPacket[] getPackets() {
       
   106 		OSCPacket[] packetArray = new OSCPacket[packets.size()];
       
   107 		packets.toArray(packetArray);
       
   108 		return packetArray;
       
   109 	}
       
   110 
       
   111 	protected void computeTimeTagByteArray(OSCJavaToByteArrayConverter stream) {
       
   112 		long millisecs = timestamp.getTime();
       
   113 		long secsSince1970 = (long) (millisecs / 1000);
       
   114 		long secs = secsSince1970 + SECONDS_FROM_1900_to_1970.longValue();
       
   115 		long picosecs = (long) (millisecs - (secsSince1970 * 1000)) * 1000;
       
   116 		
       
   117 		stream.write((int) secs);
       
   118 		stream.write((int) picosecs);
       
   119 
       
   120 	}
       
   121 
       
   122 	/**
       
   123 	 * @param stream OscPacketByteArrayConverter
       
   124 	 */
       
   125 	protected void computeByteArray(OSCJavaToByteArrayConverter stream) {
       
   126 		stream.write("#bundle");
       
   127 		computeTimeTagByteArray(stream);
       
   128 		Enumeration enm = packets.elements();
       
   129 		OSCPacket nextElement;
       
   130 		byte[] packetBytes;
       
   131 		while (enm.hasMoreElements()) {
       
   132 			nextElement = (OSCPacket) enm.nextElement();
       
   133 			packetBytes = nextElement.getByteArray();
       
   134 			stream.write(packetBytes.length);
       
   135 			stream.write(packetBytes);
       
   136 		}
       
   137 		byteArray = stream.toByteArray();
       
   138 	}
       
   139 
       
   140 }