Convert() private method

private Convert ( byte bytes, int byteCount, char chars, int charCount, bool flush, int &bytesUsed, int &charsUsed, bool &completed ) : void
bytes byte
byteCount int
chars char
charCount int
flush bool
bytesUsed int
charsUsed int
completed bool
return void
Example #1
0
        public static void Convert(this System.Text.Decoder decoder, Collections.Buffer <byte> bytes, Collections.Buffer <char> charBuffer)
        {
            int  bytesUsed;
            int  charsUsed;
            bool completed;

            decoder.Convert(bytes, charBuffer, out bytesUsed, out charsUsed, out completed);
        }
	// convert bytes to string using a fixed blocksize.
	// If something bad happens, try to recover using the
	// DecoderFallbackException info.
	private void DecoderFallbackExceptions_Convert (
		char [] chars,
		int testno,
		Decoder dec,
		DecoderFallbackExceptionTest t,
		int block_size)
	{
		int charsUsed, bytesUsed;
		bool completed;

		int ce = 0; // current exception
		for (int c = 0; c < t.bytes.Length; ) {
			try {
				int bu = c + block_size > t.bytes.Length
						? t.bytes.Length - c
						: block_size;
				dec.Convert (
					t.bytes, c, bu,
					chars, 0, chars.Length,
					c + bu >= t.bytes.Length,
					out bytesUsed, out charsUsed,
					out completed);
				c += bytesUsed;
			} catch (DecoderFallbackException ex) {
				Assert.IsTrue (
					t.eindex.Length > ce,
					String.Format (
						"test#{0}-2-{1}#{2}: UNEXPECTED FAIL (c={3}, eIndex={4}, eBytesUnknwon={5})",
						testno, block_size, ce, c,
						ex.Index,
						ex.BytesUnknown.Length));
				Assert.IsTrue (
					ex.Index + c == t.eindex[ce],
					String.Format (
						"test#{0}-2-{1}#{2}: Expected at {3} not {4}.",
						testno, block_size, ce,
						t.eindex[ce],
						ex.Index + c));
				Assert.IsTrue (
					ex.BytesUnknown.Length == t.elen[ce],
					String.Format (
						"test#{0}-2-{1}#{2}: Expected BytesUnknown.Length of {3} not {4} @{5}.",
						testno, block_size, ce,
						t.elen[0], ex.BytesUnknown.Length, c));
				for (int i = 0; i < ex.BytesUnknown.Length; i++)
					Assert.IsTrue (
						ex.BytesUnknown[i] == t.bytes[ex.Index + i + c],
						String.Format (
							"test#{0}-2-{1}#{2}: Expected byte {3:X} not {4:X} at {5}.",
							testno, block_size, ce,
							t.bytes[ex.Index + i + c],
							ex.BytesUnknown[i],
							ex.Index + i));
				c += ex.BytesUnknown.Length + ex.Index;
				dec.Reset ();
				ce++;
			}
		}
		Assert.IsTrue (
			ce == t.eindex.Length,
			String.Format (
				"test#{0}-2-{1}: UNEXPECTED SUCCESS (expected {2} exceptions, but happened {3})",
				testno, block_size, t.eindex.Length, ce));
	}
Example #3
0
 public static void Convert(this System.Text.Decoder decoder, Collections.Buffer <byte> bytes, Collections.Buffer <char> charBuffer, out int bytesUsed, out int charsUsed, out bool completed)
 {
     decoder.Convert(bytes.Array, bytes.StartOffset, bytes.Count, charBuffer.Array, charBuffer.EndOffset, charBuffer.Capacity - charBuffer.EndOffset, false, out bytesUsed, out charsUsed, out completed);
     bytes.StartOffset    += bytesUsed;
     charBuffer.EndOffset += charsUsed;
 }
 public static void Convert(Decoder decoder, ReadOnlySpan <byte> input, Span <char> output, bool flush, out int bytesRead, out int charsWritten, out bool isCompleted) => decoder.Convert(input, output, flush, out bytesRead, out charsWritten, out isCompleted);
 /// <summary>
 /// Decodes a block of input bytes into characters
 /// </summary>
 /// <param name="decoder">The decoder performing the decoding operation</param>
 /// <param name="input">The input bytes</param>
 /// <param name="output">The span that receives the output</param>
 /// <param name="flush">True to flush the decoder, otherwise False</param>
 /// <param name="bytesRead">Receives the number of bytes read</param>
 /// <param name="charsWritten">Receives the number of characters written</param>
 /// <param name="isCompleted">Receives whether all output characters have been written</param>
 public static unsafe void Convert(this Decoder decoder, ReadOnlySpan <byte> input, Span <char> output, bool flush, out int bytesRead, out int charsWritten, out bool isCompleted)
 {
     fixed(byte *pInput = &MemoryMarshal.GetReference(input))
     fixed(char *pOutput = &MemoryMarshal.GetReference(output))
     decoder.Convert(pInput, input.Length, pOutput, output.Length, flush, out bytesRead, out charsWritten, out isCompleted);
 }
                /// <summary>
                /// Convert a single character in the read buffer.
                /// </summary>
                /// <remarks>
                /// This method is designed for the ReadTo() method. One would read byte for byte, without
                /// actually updating the offsets into the buffer, building a string.
                /// <para>Normally, a single character will be read one at a time, from the first available
                /// byte until a complete string is available. The decoder used may maintain state, so the
                /// results given by this function is dependent on the state of the decoder. If a character
                /// is available, its value will be returned and on output the variable <i>bytesRead</i>
                /// will contain the number of bytes that the decoder reports as being used to obtain the
                /// single character. If no character is available in the buffer at the offset provided,
                /// -1 will be returned. The main application should therefore implement timeouts as
                /// necessary.</para>
                /// <para>Note, because of the way that decoders work, it is possible to get no characters
                /// back, but the number of bytes read via <i>bytesRead</i> may be non-zero.</para>
                /// </remarks>
                /// <param name="offset">The offset from the beginning of the read byte buffer to read from</param>
                /// <param name="decoder">The decoder to use to get a single character.</param>
                /// <param name="bytesRead">If a character is read, the number of bytes consumed to generate
                /// this character.</param>
                /// <returns>The character at the position defined by <i>offset</i>.</returns>
                public int PeekChar(int offset, Decoder decoder, out int bytesRead)
                {
                    bytesRead = 0;
                    if (m_Buffers == null) return -1;

                    // We don't lock the ReadBuffer in this case because we're not modifying the
                    // content of the ReadBuffer. We expect it only to grow and also not to
                    // shrink after reading the length, a valid assumption so long as there
                    // is no parallel Read() call during this method.
                    int readlen = m_Buffers.ReadBuffer.Length;
                    if (offset >= readlen) return -1;

                    char[] onechar = new char[1];
                    int cu = 0;
                    bool complete = false;
                    while (cu < 1 && offset < readlen) {
                        int bu;
                        decoder.Convert(m_Buffers.ReadBuffer.Array, m_Buffers.ReadBuffer.ToArrayIndex(offset), 1,
                            onechar, 0, 1, false, out bu, out cu, out complete);
                        bytesRead += bu;
                        offset++;
                    }

                    if (cu == 0) return -1;
                    return onechar[0];
                }
                /// <summary>
                /// Read data from the buffered serial stream, using the encoder given.
                /// </summary>
                /// <remarks>
                /// This function will convert to a single character using the current decoder. The
                /// decoder may maintain state from a previous Read(char[], ...) operation.
                /// <para>You may receive notification that data is available to read with the
                /// WaitForReadEvent(), but still have no data returned. This could be the case in
                /// particular if a multi-byte character is in the pipeline, but it hasn't been
                /// completely transmitted. For example, the Euro symbol is three bytes in the UTF8
                /// encoding scheme. If only one byte arrives, there is insufficient bytes to generate
                /// a single character. You should loop until a timeout occurs.</para>
                /// </remarks>
                /// <param name="decoder">The decoder to use.</param>
                /// <returns>The character interpreted, or -1 if no data available.</returns>
                public int ReadChar(Decoder decoder)
                {
                    if (m_Buffers == null) return -1;

                    int bu;
                    int cu;
                    bool completed;
                    char[] c = new char[1];

                    lock (m_ReadLock) {
                        decoder.Convert(m_Buffers.ReadBuffer, c, 0, 1, false, out bu, out cu, out completed);
                    }
                    if (cu != 1) return -1;
                    return c[0];
                }
                /// <summary>
                /// Read data from the buffered serial stream into the array provided, using the encoder
                /// given.
                /// </summary>
                /// <remarks>
                /// This function converts data in the buffered stream into the character array provided.
                /// The decoder provided is used for this conversion. The decoder may contain state from
                /// a previous encoding/decoding, so it is up to the main application to decide if this
                /// is acceptable or not.
                /// <para>The number of characters converted are returned, not the number of bytes that
                /// were consumed in the incoming buffer.</para>
                /// <para>You may receive notification that data is available to read with the
                /// WaitForReadEvent(), but still have no data returned. This could be the case in
                /// particular if a multi-byte character is in the pipeline, but it hasn't been
                /// completely transmitted. For example, the Euro symbol is three bytes in the UTF8
                /// encoding scheme. If only one byte arrives, there is insufficient bytes to generate
                /// a single character. You should loop until a timeout occurs.</para>
                /// </remarks>
                /// <param name="buffer">Buffer convert data into.</param>
                /// <param name="offset">Offset into buffer.</param>
                /// <param name="count">Number of characters to write.</param>
                /// <param name="decoder">The decoder to use.</param>
                /// <param name="bytesUsed">Number of bytes consumed by the internal read buffer.</param>
                /// <returns>Number of characters copied into the buffer.</returns>
                public int Read(char[] buffer, int offset, int count, Decoder decoder, out int bytesUsed)
                {
                    if (m_Buffers == null) {
                        bytesUsed = 0;
                        return -1;
                    }

                    int cu;
                    bool completed;

                    lock (m_ReadLock) {
                        decoder.Convert(m_Buffers.ReadBuffer, buffer, offset, count, false, out bytesUsed, out cu, out completed);
                    }
                    return cu;
                }
Example #9
0
 public override void Convert(byte* bytes, int byteCount, char* chars, int charCount, bool flush, out int bytesUsed, out int charsUsed, out bool completed) {
      decoder.Convert(bytes, byteCount, chars, charCount, flush, out bytesUsed, out charsUsed, out completed);
 }
Example #10
0
 private static string ReadStringWithUInt16LengthPrefix(byte[] buffer, ref int offset, Decoder coder)
 {
     #if DEBUG
     if (buffer == null) throw new ArgumentNullException("buffer");
     if (offset < 0 || offset > buffer.Length - 1) throw new ArgumentOutOfRangeException("offset");
     if (coder == null) throw new ArgumentNullException("coder");
     #endif
     int ofs = offset;
     int count = ReadUInt16(buffer, ref ofs);
     if (ofs + count > buffer.Length)
         throw new BadLwesDataException(String.Concat("Cannot deserialize incoming string at offset ", ofs));
     char[] result = new char[count];
     int bytesUsed;
     int charsUsed;
     bool completed;
     coder.Convert(buffer, ofs, count, result, 0, count, true
         , out bytesUsed
         , out charsUsed
         , out completed);
     if (!completed)
         throw new ArgumentException(String.Format(
             Properties.Resources.Error_BufferOutOfSpace), "buffer");
     offset = ofs + bytesUsed;
     return new String(result, 0, charsUsed);
 }