summaryrefslogtreecommitdiff
path: root/org/freedesktop/dbus/MessageWriter.java
blob: 00fb7f22484e2775d024e7262efdfae19b15d223 (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
/*
   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.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) {
		}
   }
   public void writeMessage(Message m) throws IOException
   {
      if (Debug.debug) {
         Debug.print(Debug.INFO, "<= "+m);
      }
      if (null == m) return;
      if (null == m.getWireData()) {
         if (Debug.debug) Debug.print(Debug.WARN, "Message "+m+" wire-data was null!");
         return;
      }
      if (isunix) {
         if (Debug.debug) {
            Debug.print(Debug.DEBUG, "Writing all "+m.getWireData().length+" buffers simultaneously to Unix Socket");
            for (byte[] buf: m.getWireData()) 
               Debug.print(Debug.VERBOSE, "("+buf+"):"+ (null==buf? "": Hexdump.format(buf)));
         }
         ((USOutputStream) out).write(m.getWireData());
      } else
         for (byte[] buf: m.getWireData()) {
            if (Debug.debug)
               Debug.print(Debug.VERBOSE, "("+buf+"):"+ (null==buf? "": Hexdump.format(buf)));
            if (null == buf) break;
            out.write(buf);
         }
      out.flush();
   }
   public void close() throws IOException
   {
      if (Debug.debug) Debug.print(Debug.INFO, "Closing Message Writer");
      out.close();
   }
}