📄 wireformatmessagebinary.java
字号:
throw new IllegalStateException( "WireFormatMessageBinary does not support more than 255 namespaces" ); } } /** * Builds the wire format header for the message. * * @throws IOException if for some reason the header cannot be built. */ private void buildHeader() throws IOException { ByteArrayOutputStream headerBytes = new ByteArrayOutputStream( 256 ); DataOutputStream header = new DataOutputStream(headerBytes); header.writeBytes( "jxmg" ); header.writeByte( MESSAGE_VERSION ); header.writeShort( namespaces.size() - 2 ); for( int eachNamespace = 2; eachNamespace < namespaces.size(); eachNamespace++ ) { byte [] elementName = ((String) namespaces.get(eachNamespace)).getBytes( "UTF8" ); header.writeShort( elementName.length ); header.write( elementName, 0, elementName.length ); } header.writeShort( elements.size() ); header.flush(); header.close(); headerBytes.flush(); headerBytes.close(); this.header = headerBytes.toByteArray(); } }; /** * Proxy for a message element. Handles the serialization of the element * meta information. **/ static class binaryElementProxy { final byte namespaceid; binaryElementProxy sig; MessageElement element; byte [] header; binaryElementProxy( byte namespaceid, MessageElement element ) throws IOException { this.namespaceid = namespaceid; this.element = element; MessageElement sig = element.getSignature(); if( null != sig ) { this.sig = new binaryElementProxy( namespaceid, sig ); } buildHeader(); } void buildHeader() throws IOException { byte [] elementName = element.getElementName().getBytes( "UTF8" ); byte [] elementType = null; if( !MimeMediaType.AOS.equals( element.getMimeType() ) ) { elementType = element.getMimeType().toString().getBytes( "UTF8" ); } // FIXME 20020504 bondolo@jxta.org Do something with encodings. ByteArrayOutputStream headerBytes = new ByteArrayOutputStream( 256 ); DataOutputStream header = new DataOutputStream(headerBytes); header.writeBytes( "jxel" ); header.writeByte( namespaceid ); header.writeByte( ((null != elementType) ? HAS_TYPE : 0) | ((null != sig) ? HAS_SIGNATURE : 0) ); header.writeShort( elementName.length ); header.write( elementName, 0, elementName.length ); if( null != elementType ) { header.writeShort( elementType.length ); header.write( elementType, 0, elementType.length ); } // FIXME content encoding should go here long dataLen = element.getByteLength(); if( dataLen > Integer.MAX_VALUE ) { throw new IllegalStateException( "WireFormatMessageBinary does not support elements longer than 4GB" ); } header.writeInt( (int) dataLen ); header.flush(); header.close(); headerBytes.flush(); headerBytes.close(); this.header = headerBytes.toByteArray(); } public long getByteLength() { long size = 0; size += header.length; size += element.getByteLength(); if ( null != sig ) { size += sig.getByteLength(); } return size; } public InputStream getStream() throws IOException { List streamParts = new ArrayList(); streamParts.add( new ByteArrayInputStream( header ) ); streamParts.add( element.getStream() ); if( null != sig ) { streamParts.add( sig.getStream() ); } return new SequenceInputStream( Collections.enumeration( streamParts ) ); } public void sendToStream( OutputStream sendTo ) throws IOException { sendTo.write( header ); element.sendToStream( sendTo ); if( null != sig ) { sig.sendToStream( sendTo ); } } }; /** * The message we are serializing. **/ private final Message msg; /** * The mod count of the message when we started. Used for detecting * (illegal) modifications. **/ private final int msgModCount; /** * The mime type of the encoded message. **/ private final MimeMediaType type; /** * The mime type of the content encoding for this message. **/ private final MimeMediaType contentEncoding; /** * The serialization peer to the Message. **/ private final binaryMessageProxy msgProxy; /** * Creates a new instance of WireFormatMessageBinary. Called only by the * Instantiator. * * @param msg the message being serialized * @param type the mime mediatype being requested. * @param preferedContentEncodings The ranked content encodings prefered by * the recipient. */ WireFormatMessageBinary( Message msg, MimeMediaType type, MimeMediaType[] preferedContentEncodings ) throws IOException { if( null == msg ) { throw new IllegalArgumentException( "Null message!" ); } this.msg = msg; this.msgModCount = msg.getMessageModCount(); if( null == type ) { throw new IllegalArgumentException( "Null mime type!" ); } int matchedIdx = -1; for( int eachType = 0; eachType < myTypes.length; eachType++ ) { if( type.equalsIngoringParams( myTypes[eachType] ) ) { matchedIdx = eachType; break; } } if( -1 == matchedIdx ) { throw new IllegalArgumentException( "Unsupported mime type!" ); } // FIXME 20020504 bondolo@jxta.org Check the mimetype params to make // sure we can support them. this.type = type; // FIXME 20020504 bondolo@jxta.org Do something with encodings. this.contentEncoding = myContentEncodings[0]; msgProxy = new binaryMessageProxy( msg, type ); } /** * {@inheritDoc} **/ public boolean equals(Object obj) { throw new UnsupportedOperationException( "don't do this" ); } /* * The cost of just having a finalize routine is high. The finalizer is * a bottleneck and can delay garbage collection all the way to heap * exhaustion. Leave this comment as a reminder to future maintainers. * Below is the reason why finalize is not needed here. * * No critical non-memory resource held. protected void finalize( ) { } */ /** * {@inheritDoc} **/ public int hashCode() { throw new UnsupportedOperationException( "don't do this" ); } /** * {@inheritDoc} **/ public String getFileExtension() { return "???"; } /** * {@inheritDoc} **/ public MimeMediaType getMimeType() { return type; } /** * {@inheritDoc} **/ public InputStream getStream() throws IOException { if( msg.getMessageModCount() != msgModCount ) { throw new IllegalStateException( "message was unexpectedly modified!" ); } msg.modifiable = false; try { InputStream result = msgProxy.getStream(); return result; } finally { msg.modifiable = true; } } /** * {@inheritDoc} **/ public void sendToStream( OutputStream sendTo ) throws IOException { if( msg.getMessageModCount() != msgModCount ) { throw new IllegalStateException( "message was unexpectedly modified!" ); } msg.modifiable = false; try { msgProxy.sendToStream( sendTo ); } finally { msg.modifiable = true; } } /** * {@inheritDoc} **/ public long getByteLength() { if( msg.getMessageModCount() != msgModCount ) { throw new IllegalStateException( "message was unexpectedly modified!" ); } return msgProxy.getByteLength(); } /** * {@inheritDoc} **/ public MimeMediaType getContentEncoding() { return contentEncoding; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -