📄 message.java
字号:
/* I BlueTooth You -- Simple BlueTooth talker Copyright (C) 2007 Jan Tomka This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. */package net.sf.btw.ibtu;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.io.IOException;import java.util.Vector;import net.sf.btw.tools.Logger;/** * This class defines an IBTU message with sender identification and the message * body itself. * * @author Jan Tomka TODO Hide and encapsulate the member variables. TODO Line * splitting and line counting should be split apart to separate class, * e.g. HistoryMessage. */public final class Message { /** * Sender identificator of system message. TODO Update where needed, to use * this constant. */ public static final String SYSTEM = null; /** * Constructs an empty message with <code>null</code> sender (a system * message). TODO Remove this constructor, don't allow empty messages. */ public Message() { super(); } /** * Constructs a message. * * @param sender * Sender of the message. Value <code>null</code> indicates * system messages. * @param body * Body of the message. Must not be <code>null</code>. TODO * Strip sender and body string, don't allow null or empty body. */ public Message(final String sender, final String body) { super(); this.sender = sender; this.body = body; } /** * Sender of the message. Value <code>null</code> indicates system * messages. */ public String sender; /** * Returns sender of a message. * * @returns Sender of a message. */ public String getSender() { return sender; } /** * Body of the message. Must not be <code>null</code>. */ public String body = ""; /** * Returns body of a message. * * @return Body of a message. */ public String getBody() { return body; } /** * Vector of {@link String}s the message sender string is split to, * depending on the device display width. */ public transient Vector senderLines; /** * Vector of {@link String}s the message body string is split to, depending * on the device display width. */ public transient Vector bodyLines; /** * Splits sender and body strings to individual lines. Does it so that each * of the resulting lines would fit on a line of given width, drawn with * given font. * * @param f * Font to use when splitting to individual lines. * @param width * Horizontal width available for each line. */ /* * TODO Move HistoryCanvas.splitLines here. public void splitToLines(Font f, * int width) { } */ /** * Creates communication package from given pair. * * @param sender * sender, if <code>null</code> then the message is from the * system. * @param message * the message. * @return byte array representation. */ public byte[] toByteArray() { final ByteArrayOutputStream bout = new ByteArrayOutputStream(body .length() + 14); final DataOutputStream out = new DataOutputStream(bout); try { out.writeUTF(sender == null ? "" : sender); out.writeUTF(body); out.close(); } catch (IOException ex) { // shouldn't happen Logger.error("Message.toByteArray()", ex); } return bout.toByteArray(); } /** * Reads message object from given stream. * * @param stream * stream to read from. The stream is not closed. * @return message instance * @throws IOException * if decoding error happens. */ public static Message fromStream(DataInputStream stream) throws IOException { final Message result = new Message(); result.sender = stream.readUTF(); if (result.sender.length() == 0) result.sender = null; result.body = stream.readUTF(); return result; } /** * Checks if this message is a system message. * * @return <code>true</code> if {@link #sender} is <code>null</code>. */ public boolean isSystemMessage() { return sender == null; } /** * Counts lines in a {@link Vector} of {@link Message}'s. * * @param msgs * Vector of Message's. * @return Number of lines. TODO Make splitToLines caluculate this once and * keep track of current line count, and line index to start sending * the recent history from, as well. */ public static int countLines(Vector msgs) { int result = 0; for (int i = 0; i < msgs.size(); i++) { int senderLineCount = ((Message) msgs.elementAt(i)).senderLines .size(); int bodyLineCount = ((Message) msgs.elementAt(i)).bodyLines.size(); result += ((senderLineCount == 0) ? 0 : (senderLineCount - 1)) + bodyLineCount; } return result; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -