Code:
/ WCF / WCF / 3.5.30729.1 / untmp / Orcas / SP / ndp / cdf / src / WCF / ServiceModel / System / ServiceModel / Channels / MessageEncoder.cs / 1 / MessageEncoder.cs
//------------------------------------------------------------------------------ // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- namespace System.ServiceModel.Channels { using System; using System.IO; using System.Net.Mime; using System.Runtime.Serialization; using System.ServiceModel.Diagnostics; public abstract class MessageEncoder { public abstract string ContentType { get; } public abstract string MediaType { get; } public abstract MessageVersion MessageVersion { get; } public virtual T GetProperty() where T : class { if (typeof(T) == typeof(FaultConverter)) { return (T)(object)FaultConverter.GetDefaultFaultConverter(this.MessageVersion); } return null; } public Message ReadMessage(Stream stream, int maxSizeOfHeaders) { return ReadMessage(stream, maxSizeOfHeaders, null); } public abstract Message ReadMessage(Stream stream, int maxSizeOfHeaders, string contentType); public Message ReadMessage(ArraySegment buffer, BufferManager bufferManager) { return ReadMessage(buffer, bufferManager, null); } public abstract Message ReadMessage(ArraySegment buffer, BufferManager bufferManager, string contentType); // used for buffered streaming internal ArraySegment BufferMessageStream(Stream stream, BufferManager bufferManager, int maxBufferSize) { byte[] buffer = bufferManager.TakeBuffer(ConnectionOrientedTransportDefaults.ConnectionBufferSize); int offset = 0; int currentBufferSize = Math.Min(buffer.Length, maxBufferSize); while (offset < currentBufferSize) { int count = stream.Read(buffer, offset, currentBufferSize - offset); if (count == 0) { stream.Close(); break; } offset += count; if (offset == currentBufferSize) { if (currentBufferSize >= maxBufferSize) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(MaxMessageSizeStream.CreateMaxReceivedMessageSizeExceededException(maxBufferSize)); } currentBufferSize = Math.Min(currentBufferSize * 2, maxBufferSize); byte[] temp = bufferManager.TakeBuffer(currentBufferSize); Buffer.BlockCopy(buffer, 0, temp, 0, offset); bufferManager.ReturnBuffer(buffer); buffer = temp; } } return new ArraySegment (buffer, 0, offset); } // used for buffered streaming internal virtual Message ReadMessage(Stream stream, BufferManager bufferManager, int maxBufferSize, string contentType) { return ReadMessage(BufferMessageStream(stream, bufferManager, maxBufferSize), bufferManager, contentType); } public override string ToString() { return ContentType; } public abstract void WriteMessage(Message message, Stream stream); public ArraySegment WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager) { return WriteMessage(message, maxMessageSize, bufferManager, 0); } public abstract ArraySegment WriteMessage(Message message, int maxMessageSize, BufferManager bufferManager, int messageOffset); public virtual bool IsContentTypeSupported(string contentType) { if (contentType == null) throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("contentType")); return IsContentTypeSupported(contentType, this.ContentType, this.MediaType); } internal bool IsContentTypeSupported(string contentType, string supportedContentType, string supportedMediaType) { if (supportedContentType == contentType) return true; if (contentType.Length > supportedContentType.Length && contentType.StartsWith(supportedContentType, StringComparison.Ordinal) && contentType[supportedContentType.Length] == ';') return true; // now check case-insensitively if (contentType.StartsWith(supportedContentType, StringComparison.OrdinalIgnoreCase)) { if (contentType.Length == supportedContentType.Length) { return true; } else if (contentType.Length > supportedContentType.Length) { char ch = contentType[supportedContentType.Length]; // Linear Whitespace is allowed to appear between the end of one property and the semicolon. // LWS = [CRLF]? (SP | HT)+ if (ch == ';') { return true; } // Consume the [CRLF]? int i = supportedContentType.Length; if (ch == '\r' && contentType.Length > supportedContentType.Length + 1 && contentType[i + 1] == '\n') { i += 2; ch = contentType[i]; } // Look for a ';' or nothing after (SP | HT)+ if (ch == ' ' || ch == '\t') { i++; while (i < contentType.Length) { ch = contentType[i]; if (ch != ' ' && ch != '\t') break; ++i; } } if (ch == ';' || i == contentType.Length) return true; } } // sometimes we get a contentType that has parameters, but our encoders // merely expose the base content-type, so we will check a stripped version try { ContentType parsedContentType = new ContentType(contentType); if (supportedMediaType.Length > 0 && !supportedMediaType.Equals(parsedContentType.MediaType, StringComparison.OrdinalIgnoreCase)) return false; if (!IsCharSetSupported(parsedContentType.CharSet)) return false; } catch (FormatException) { // bad content type, so we definitely don't support it! return false; } return true; } internal virtual bool IsCharSetSupported(string charset) { return false; } internal void ThrowIfMismatchedMessageVersion(Message message) { if (message.Version != MessageVersion) { throw TraceUtility.ThrowHelperError( new ProtocolException(SR.GetString(SR.EncoderMessageVersionMismatch, message.Version, MessageVersion)), message); } } } } // File provided for Reference Use Only by Microsoft Corporation (c) 2007. // Copyright (c) Microsoft Corporation. All rights reserved.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- FixedSOMPageConstructor.cs
- EndOfStreamException.cs
- FileDialog.cs
- HashAlgorithm.cs
- ToolStripControlHost.cs
- EntityException.cs
- StorageEndPropertyMapping.cs
- MediaContextNotificationWindow.cs
- XPathExpr.cs
- DnsPermission.cs
- PerfService.cs
- SiteMapDataSourceDesigner.cs
- Debug.cs
- RegistryConfigurationProvider.cs
- WSSecurityPolicy11.cs
- MulticastNotSupportedException.cs
- ClientProxyGenerator.cs
- WebPartConnectVerb.cs
- OutputCacheSettingsSection.cs
- QilXmlWriter.cs
- SoapObjectInfo.cs
- SmtpAuthenticationManager.cs
- SoapIgnoreAttribute.cs
- DBConcurrencyException.cs
- InvokeBase.cs
- CodeRegionDirective.cs
- MergeLocalizationDirectives.cs
- TdsParserSafeHandles.cs
- SimpleWorkerRequest.cs
- Base64Encoder.cs
- Events.cs
- DesignerValidationSummaryAdapter.cs
- ScrollProviderWrapper.cs
- SecurityContext.cs
- RenderContext.cs
- AmbientLight.cs
- XmlSchemaParticle.cs
- UniqueConstraint.cs
- AspProxy.cs
- Control.cs
- XhtmlConformanceSection.cs
- ValidationSummaryDesigner.cs
- TraceHandlerErrorFormatter.cs
- BuildProviderAppliesToAttribute.cs
- GlyphManager.cs
- UnlockInstanceCommand.cs
- ExpressionNode.cs
- OwnerDrawPropertyBag.cs
- HandledEventArgs.cs
- SmiGettersStream.cs
- CodeGeneratorOptions.cs
- MediaContext.cs
- DelimitedListTraceListener.cs
- FreezableDefaultValueFactory.cs
- ModifiableIteratorCollection.cs
- DesignerDataParameter.cs
- QilLiteral.cs
- SecurityTokenRequirement.cs
- WpfXamlLoader.cs
- UserPreferenceChangedEventArgs.cs
- CacheAxisQuery.cs
- MethodAccessException.cs
- IgnorePropertiesAttribute.cs
- TableLayoutCellPaintEventArgs.cs
- Atom10FormatterFactory.cs
- EntityDataSourceDesignerHelper.cs
- PropertyKey.cs
- ACL.cs
- COM2PropertyDescriptor.cs
- PermissionListSet.cs
- SamlAssertionDirectKeyIdentifierClause.cs
- FixedSOMTextRun.cs
- Int32AnimationBase.cs
- TextTreeInsertElementUndoUnit.cs
- SplineQuaternionKeyFrame.cs
- DNS.cs
- ClrPerspective.cs
- Parameter.cs
- XmlCharCheckingReader.cs
- DetailsViewRow.cs
- SimpleHandlerFactory.cs
- VersionPair.cs
- HtmlSelect.cs
- DataSourceSelectArguments.cs
- DrawingContext.cs
- RegisteredDisposeScript.cs
- ProbeMatchesMessage11.cs
- Event.cs
- __ComObject.cs
- SweepDirectionValidation.cs
- OutputCacheSettingsSection.cs
- EventItfInfo.cs
- IntSecurity.cs
- PermissionToken.cs
- OverloadGroupAttribute.cs
- URIFormatException.cs
- COM2EnumConverter.cs
- ProxySimple.cs
- ThousandthOfEmRealPoints.cs
- _emptywebproxy.cs