summaryrefslogtreecommitdiff
path: root/src/traffic-incidents-service/org.genivi.trafficinfo.dbus-java/src/main/java/org/freedesktop/dbus/MessageWriter.java
blob: bb221080a628e6f72d8e9c81e0110dd3f69e5d6d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
   D-Bus Java Implementation
   Copyright (c) 2005-2006 Matthew Johnson

   This program is free software; you can redistribute it and/or modify it
   under the terms of either the GNU Lesser General Public License Version 2 or the
   Academic Free Licence Version 2.1.

   Full licence texts are included in the COPYING file with this program.
*/
package org.freedesktop.dbus;

import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.io.IOException;

import cx.ath.matthew.debug.Debug;
import cx.ath.matthew.unix.USOutputStream;
import cx.ath.matthew.utils.Hexdump;

public class MessageWriter
{
   private OutputStream out;
	private boolean isunix;
   public MessageWriter(OutputStream out)
   {
		this.out = out;
		this.isunix = false;
		try {
			if (out instanceof USOutputStream)
				this.isunix = true;
		} catch (Throwable t) {
		}
		if (!this.isunix)
			this.out = new BufferedOutputStream(this.out);
   }
   public void writeMessage(Message m) throws IOException
   {
      if (Debug.debug) {
         Debug.print(Debug.INFO, "<= "+m);
      }
      if (null == m) return;
      if (Debug.debug) Debug.print(Debug.INFO, "m in not null");  // pego
      if (null == m.getWireData()) {
         if (Debug.debug) Debug.print(Debug.WARN, "Message "+m+" wire-data was null!");
         return;
      }
      if (Debug.debug) Debug.print(Debug.INFO, "wire data in not null");  // pego
      if (isunix) {
         if (Debug.debug) {
            Debug.print(Debug.INFO, "Writing all "+m.getWireData().length+" buffers simultaneously to Unix Socket");
            for (byte[] buf: m.getWireData()) 
               Debug.print(Debug.INFO, "("+buf+"):"+ (null==buf? "": Hexdump.format(buf)));
         }
         ((USOutputStream) out).write(m.getWireData());
      } else
         for (byte[] buf: m.getWireData()) {
            if (Debug.debug)
               Debug.print(Debug.INFO, "("+buf+"):"+ (null==buf? "": Hexdump.format(buf)));
            if (null == buf) break;
            out.write(buf);
            if (Debug.debug) Debug.print(Debug.INFO, "After write()");  // pego
         }
      out.flush();
      if (Debug.debug) Debug.print(Debug.INFO, "After flush()");  // pego
   }
   public void close() throws IOException
   {
      if (Debug.debug) Debug.print(Debug.INFO, "Closing Message Writer");
      out.close();
   }
}