?? messagetutorial.java
字號:
* @return The long value * @throws NumberFormatException If the String does not contain a parsable int. */ public static long getLongFromMessage(Message message, String nameSpace, String elemName) throws NumberFormatException { String longStr = getStringFromMessage(message, nameSpace, elemName); if (null != longStr) { return Long.parseLong(longStr); } else { throw new NumberFormatException("No such Message Element."); } } /** * Returns an int from a message * * @param message The message to retrieve from * @param nameSpace The namespace of the element to get. * @param elemName Name of the Element. * @return The int value * @throws NumberFormatException If the String does not contain a parsable long. */ public static int getIntegerFromMessage(Message message, String nameSpace, String elemName) throws NumberFormatException { String intStr = getStringFromMessage(message, nameSpace, elemName); if (null != intStr) { return Integer.parseInt(intStr); } else { throw new NumberFormatException("No such Message Element."); } } /** * Returns an InputStream for a byte array * * @param message The message to retrieve from * @param nameSpace The namespace of the element to get. * @param elemName Name of the Element. * @return The {@code InputStream} or {@code null} if the message has no such element, String elemName) throws IOException { * @throws IOException if an io error occurs */ public static InputStream getInputStreamFromMessage(Message message, String nameSpace, String elemName) throws IOException { InputStream result = null; MessageElement element = message.getMessageElement(nameSpace, elemName); if (null == element) { return null; } if (element.getMimeType().equals(GZIP_MEDIA_TYPE)) { result = new GZIPInputStream(element.getStream()); } else if (element.getMimeType().equals(MimeMediaType.AOS)) { result = element.getStream(); } return result; } /** * Reads a single Java Object from a Message. * * @param message The message containing the object. * @param nameSpace The name space of the element containing the object. * @param elemName The name of the element containing the object. * @return The Object or {@code null} if the Message contained no such element. * @throws IOException if an io error occurs * @throws ClassNotFoundException if an object could not constructed from the message element */ public static Object getObjectFromMessage(Message message, String nameSpace, String elemName) throws IOException, ClassNotFoundException { InputStream is = getInputStreamFromMessage(message, nameSpace, elemName); if (null == is) { return null; } ObjectInputStream ois = new ObjectInputStream(is); return ois.readObject(); } /** * Prints message element names and content and some stats * * @param msg message to print * @param verbose indicates whether to print elment content */ public static void printMessageStats(Message msg, boolean verbose) { try { System.out.println("------------------Begin Message---------------------"); WireFormatMessage serialed = WireFormatMessageFactory.toWire(msg, new MimeMediaType("application/x-jxta-msg"), null); System.out.println("Message Size :" + serialed.getByteLength()); ElementIterator it = msg.getMessageElements(); while (it.hasNext()) { MessageElement el = it.next(); System.out.println("Element : " + it.getNamespace() + " :: " + el.getElementName()); if (verbose) { System.out.println("[" + el + "]"); } } System.out.println("-------------------End Message----------------------"); } catch (Exception e) { e.printStackTrace(); } } /** * Illustrates adding and retrieving a String to and from a Message */ public static void stringExample() { Message message = new Message(); addStringToMessage(message, "TutorialNameSpace", "String Test", "This is a test"); printMessageStats(message, true); System.out.println("String Value :" + getStringFromMessage(message, "TutorialNameSpace", "String Test")); } /** * Illustrates adding and retrieving a long to and from a Message */ public static void longExample() { Message message = new Message(); addLongToMessage(message, "TutorialNameSpace", "long test", Long.MAX_VALUE); printMessageStats(message, true); System.out.println("long Value :" + getLongFromMessage(message, "TutorialNameSpace", "long test")); } /** * Illustrates adding and retrieving an integer to and from a Message */ public static void intExample() { Message message = new Message(); addIntegerToMessage(message, "TutorialNameSpace", "int test", Integer.MAX_VALUE); printMessageStats(message, true); System.out.println("int Value :" + getIntegerFromMessage(message, "TutorialNameSpace", "int test")); } /** * Illustrates adding and retrieving byte-array to and from a Message */ public static void byteArrayExample() { Message message = new Message(); try { File file = new File("message.tst"); file.createNewFile(); RandomAccessFile raf = new RandomAccessFile(file, "rw"); raf.setLength(1024 * 4); int size = 4096; byte[] buf = new byte[size]; raf.read(buf, 0, size); addByteArrayToMessage(message, "TutorialNameSpace", "byte test", buf, true); printMessageStats(message, true); InputStream is = getInputStreamFromMessage(message, "TutorialNameSpace", "byte test"); int count = 0; while (is.read() != -1) { count++; } System.out.println("Read " + count + " byte back"); } catch (IOException io) { io.printStackTrace(); } } /** * Illustrates adding and retrieving advertisements to and from a Message */ public static void xmlDocumentExample() { Message message = new Message(); PipeAdvertisement pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(PipeAdvertisement.getAdvertisementType()); pipeAdv.setPipeID(IDFactory.newPipeID(PeerGroupID.defaultNetPeerGroupID)); pipeAdv.setType(PipeService.UnicastType); message.addMessageElement("MESSAGETUT", new TextDocumentMessageElement("MESSAGETUT", (XMLDocument) pipeAdv.getDocument(MimeMediaType.XMLUTF8), null)); MessageElement msgElement = message.getMessageElement("MESSAGETUT", "MESSAGETUT"); try { XMLDocument asDoc = (XMLDocument) StructuredDocumentFactory.newStructuredDocument(msgElement.getMimeType(), msgElement.getStream()); PipeAdvertisement newPipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(asDoc); System.out.println(newPipeAdv.toString()); } catch (IOException e) { // This is thrown if the message element could not be read. e.printStackTrace(); } catch (IllegalArgumentException e) { // This is thrown if the document or advertisement is invalid (illegal values, missing tags, etc.) e.printStackTrace(); } } /** * Main method * * @param args command line arguments. None defined */ public static void main(String args[]) { stringExample(); longExample(); intExample(); byteArrayExample(); xmlDocumentExample(); }}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -