package binary;

import implementation.ClientJMS;

import java.io.File;
import java.io.FileInputStream;

import javax.jms.BytesMessage;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;

import javax.jms.Session;



/* Annick Fron
 * Montre comment envoyer du binaire dans un message
 */


public class Emetteur extends ClientJMS {


	public static void main(String[] args) {
	
		new Emetteur().run();
	}
	public void run() {

		try {
			ConnectionFactory cf=getConnectionFactory();
			Connection connexion= cf.createConnection();
			Session session = connexion.createSession(false, Session.AUTO_ACKNOWLEDGE);
			Destination queue = session.createQueue("SampleQ1");
			MessageProducer sender = session.createProducer(queue);
			BytesMessage outMessage = session.createBytesMessage();
			byte[] buffer=readFile("photo.gif");
			outMessage.writeBytes(buffer);
			outMessage.setIntProperty("size",buffer.length);
			sender.send( outMessage);
			session.close();
			connexion.close();
			System.out.println("Message envoye");


		} catch (JMSException e) {
		
			e.printStackTrace();
		}
	}
	

	public static byte[] readFile(String inputFilename) {
	  	try{ File inputFile = new File(inputFilename);
	           FileInputStream in = new FileInputStream(inputFile);
	           byte bt[] = new byte[(int)inputFile.length()];
	           in.read(bt);
	           return bt;}
	           catch (Exception e) {e.printStackTrace();return null;}
	  }
}
