public override void TestPutCharBuffer() { CharBuffer other = CharBuffer.Allocate(1); try { buf.Put(other); fail("Should throw ReadOnlyBufferException"); //$NON-NLS-1$ } catch (ReadOnlyBufferException e) { // expected } try { buf.Put((CharBuffer)null); fail("Should throw NullPointerException"); //$NON-NLS-1$ } catch (ArgumentNullException e) { // expected } try { buf.Put(buf); fail("Should throw IllegalArgumentException"); //$NON-NLS-1$ } catch (ArgumentException e) { // expected } }
public override void SetUp() { base.SetUp(); buf = CharBuffer.Allocate(BUFFER_LENGTH); loadTestData1(buf); baseBuf = buf; }
private static void TestAllocate() { // An IllegalArgumentException will be thrown for negative capacities. tryCatch((Buffer)null, typeof(ArgumentException), () => { CharBuffer.Allocate(-1); }); }
/// <summary> /// Tells whether or not this encoder can encode the given character. /// /// <para> This method returns <tt>false</tt> if the given character is a /// surrogate character; such characters can be interpreted only when they /// are members of a pair consisting of a high surrogate followed by a low /// surrogate. The {@link #canEncode(java.lang.CharSequence) /// canEncode(CharSequence)} method may be used to test whether or not a /// character sequence can be encoded. /// /// </para> /// <para> This method may modify this encoder's state; it should therefore not /// be invoked if an <a href="#steps">encoding operation</a> is already in /// progress. /// /// </para> /// <para> The default implementation of this method is not very efficient; it /// should generally be overridden to improve performance. </para> /// </summary> /// <param name="c"> /// The given character /// </param> /// <returns> <tt>true</tt> if, and only if, this encoder can encode /// the given character /// </returns> /// <exception cref="IllegalStateException"> /// If an encoding operation is already in progress </exception> public virtual bool CanEncode(char c) { CharBuffer cb = CharBuffer.Allocate(1); cb.Put(c); cb.Flip(); return(CanEncode(cb)); }
/// <summary> /// Internal scope constructor. /// </summary> internal WordData(Encoding decoder) { this.decoder = decoder; stemBuffer = ByteBuffer.Allocate(0); tagBuffer = ByteBuffer.Allocate(0); stemCharSequence = CharBuffer.Allocate(0); tagCharSequence = CharBuffer.Allocate(0); }
public static void Test() { TestAllocate(); test(0, CharBuffer.Allocate(7 * 1024), false); test(0, CharBuffer.Wrap(new char[7 * 1024], 0, 7 * 1024), false); test(new char[1024]); testStr(); callReset(CharBuffer.Allocate(10)); putBuffer(); }
/// <summary> /// Ensure the buffer's capacity is large enough to hold a given number /// of elements. If the input buffer is not large enough, a new buffer is allocated /// and returned. /// </summary> /// <param name="buffer">The buffer to check or <c>null</c> if a new buffer should be allocated.</param> /// <param name="elements">The required number of elements to be appended to the buffer.</param> /// <returns>Returns the same buffer or a new buffer with the given capacity.</returns> public static CharBuffer ClearAndEnsureCapacity(CharBuffer buffer, int elements) { if (buffer == null || buffer.Capacity < elements) { buffer = CharBuffer.Allocate(elements); } else { buffer.Clear(); } return(buffer); }
public void TestAllocatedCharBuffer_IllegalArg() { try { CharBuffer.Allocate(-1); fail("Should throw Exception"); //$NON-NLS-1$ } catch (ArgumentException e) { // expected } }
private static void bulkPutBuffer(CharBuffer b) { int n = b.Capacity; b.Clear(); CharBuffer c = CharBuffer.Allocate(n + 7); c.Position = (7); for (int i = 0; i < n; i++) { c.Put((char)Ic(i)); } c.Flip(); c.Position = (7); b.Put(c); b.Flip(); }
/// <summary> /// Tells whether or not the given byte array is a legal replacement value /// for this encoder. /// /// <para> A replacement is legal if, and only if, it is a legal sequence of /// bytes in this encoder's charset; that is, it must be possible to decode /// the replacement into one or more sixteen-bit Unicode characters. /// /// </para> /// <para> The default implementation of this method is not very efficient; it /// should generally be overridden to improve performance. </para> /// </summary> /// <param name="repl"> The byte array to be tested /// </param> /// <returns> <tt>true</tt> if, and only if, the given byte array /// is a legal replacement value for this encoder </returns> public virtual bool IsLegalReplacement(sbyte[] repl) { WeakReference <CharsetDecoder> wr = CachedDecoder; CharsetDecoder dec = null; if ((wr == null) || ((dec = wr.get()) == null)) { dec = Charset().NewDecoder(); dec.OnMalformedInput(CodingErrorAction.REPORT); dec.OnUnmappableCharacter(CodingErrorAction.REPORT); CachedDecoder = new WeakReference <CharsetDecoder>(dec); } else { dec.Reset(); } ByteBuffer bb = ByteBuffer.Wrap(repl); CharBuffer cb = CharBuffer.Allocate((int)(bb.Remaining() * dec.MaxCharsPerByte())); CoderResult cr = dec.Decode(bb, cb, true); return(!cr.Error); }
/// <exception cref="System.IO.IOException"></exception> private int AppendDecoded(CharArrayBuffer charbuffer, ByteBuffer bbuf) { if (!bbuf.HasRemaining()) { return(0); } if (this.cbuf == null) { this.cbuf = CharBuffer.Allocate(1024); } this.decoder.Reset(); int len = 0; while (bbuf.HasRemaining()) { CoderResult result = this.decoder.Decode(bbuf, this.cbuf, true); len += HandleDecodingResult(result, charbuffer, bbuf); } CoderResult result_1 = this.decoder.Flush(this.cbuf); len += HandleDecodingResult(result_1, charbuffer, bbuf); this.cbuf.Clear(); return(len); }
/// <summary> /// Convenience method that decodes the remaining content of a single input /// byte buffer into a newly-allocated character buffer. /// /// <para> This method implements an entire <a href="#steps">decoding /// operation</a>; that is, it resets this decoder, then it decodes the /// bytes in the given byte buffer, and finally it flushes this /// decoder. This method should therefore not be invoked if a decoding /// operation is already in progress. </para> /// </summary> /// <param name="in"> /// The input byte buffer /// </param> /// <returns> A newly-allocated character buffer containing the result of the /// decoding operation. The buffer's position will be zero and its /// limit will follow the last character written. /// </returns> /// <exception cref="IllegalStateException"> /// If a decoding operation is already in progress /// </exception> /// <exception cref="MalformedInputException"> /// If the byte sequence starting at the input buffer's current /// position is not legal for this charset and the current malformed-input action /// is <seealso cref="CodingErrorAction#REPORT"/> /// </exception> /// <exception cref="UnmappableCharacterException"> /// If the byte sequence starting at the input buffer's current /// position cannot be mapped to an equivalent character sequence and /// the current unmappable-character action is {@link /// CodingErrorAction#REPORT} </exception> //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in .NET: //ORIGINAL LINE: public final java.nio.CharBuffer decode(java.nio.ByteBuffer in) throws CharacterCodingException public CharBuffer Decode(ByteBuffer @in) { int n = (int)(@in.Remaining() * AverageCharsPerByte()); CharBuffer @out = CharBuffer.Allocate(n); if ((n == 0) && (@in.Remaining() == 0)) { return(@out); } Reset(); for (;;) { CoderResult cr = @in.HasRemaining() ? Decode(@in, @out, true) : CoderResult.UNDERFLOW; if (cr.Underflow) { cr = Flush(@out); } if (cr.Underflow) { break; } if (cr.Overflow) { n = 2 * n + 1; // Ensure progress; n might be 0! CharBuffer o = CharBuffer.Allocate(n); @out.Flip(); o.Put(@out); @out = o; continue; } cr.ThrowException(); } @out.Flip(); return(@out); }
public static void test(int level, CharBuffer b, bool direct) { Show(level, b); //if (direct != b.IsDirect) // J2N: IsDirect not implemented // fail("Wrong direction", b); // Gets and puts relPut(b); relGet(b); absGet(b); bulkGet(b); absPut(b); relGet(b); absGet(b); bulkGet(b); bulkPutArray(b); relGet(b); bulkPutBuffer(b); relGet(b); bulkPutString(b); relGet(b); b.Position = (1); b.Limit = (7); ck(b, b.ToString().Equals("bcdefg")); // CharSequence ops b.Position = (2); ck(b, b[1], 'd'); CharBuffer c = b.Subsequence(1, 4 - 1); // J2N: end - start ck(c, c.Capacity, b.Capacity); ck(c, c.Position, b.Position + 1); ck(c, c.Limit, b.Position + 4); ck(c, b.Subsequence(1, 4 - 1).ToString().Equals("def")); // J2N: end - start // 4938424 b.Position = (4); ck(b, b[1], 'f'); ck(b, b.Subsequence(1, 3 - 1).ToString().Equals("fg")); // J2N: end - start // Compact relPut(b); b.Position = (13); b.Compact(); b.Flip(); relGet(b, 13); // Exceptions relPut(b); b.Limit = (b.Capacity / 2); b.Position = (b.Limit); tryCatch(b, typeof(BufferUnderflowException), () => { b.Get(); }); tryCatch(b, typeof(BufferOverflowException), () => { b.Put((char)42); }); // The index must be non-negative and lesss than the buffer's limit. tryCatch(b, typeof(ArgumentOutOfRangeException), () => { b.Get(b.Limit); }); tryCatch(b, typeof(ArgumentOutOfRangeException), () => { b.Get(-1); }); tryCatch(b, typeof(ArgumentOutOfRangeException), () => { b.Put(b.Limit, (char)42); }); tryCatch(b, typeof(InvalidMarkException), () => { b.Position = (0); b.Mark(); b.Compact(); b.Reset(); }); // Values b.Clear(); b.Put((char)0); b.Put(unchecked ((char)-1)); b.Put((char)1); b.Put(char.MaxValue); b.Put(char.MinValue); char v; b.Flip(); ck(b, b.Get(), 0); ck(b, b.Get(), unchecked ((char)-1)); ck(b, b.Get(), 1); ck(b, b.Get(), char.MaxValue); ck(b, b.Get(), char.MinValue); // Comparison b.Rewind(); CharBuffer b2 = CharBuffer.Allocate(b.Capacity); b2.Put(b); b2.Flip(); b.Position = (2); b2.Position = (2); if (!b.Equals(b2)) { for (int i = 2; i < b.Limit; i++) { char x = b.Get(i); char y = b2.Get(i); if (x != y ) { output.WriteLine("[" + i + "] " + x + " != " + y); } } fail("Identical buffers not equal", b, b2); } if (b.CompareTo(b2) != 0) { fail("Comparison to identical buffer != 0", b, b2); } b.Limit = (b.Limit + 1); b.Position = (b.Limit - 1); b.Put((char)99); b.Rewind(); b2.Rewind(); if (b.Equals(b2)) { fail("Non-identical buffers equal", b, b2); } if (b.CompareTo(b2) <= 0) { fail("Comparison to shorter buffer <= 0", b, b2); } b.Limit = (b.Limit - 1); b.Put(2, (char)42); if (b.Equals(b2)) { fail("Non-identical buffers equal", b, b2); } if (b.CompareTo(b2) <= 0) { fail("Comparison to lesser buffer <= 0", b, b2); } // Check equals and compareTo with interesting values foreach (char x in VALUES) { CharBuffer xb = CharBuffer.Wrap(new char[] { x }); if (xb.CompareTo(xb) != 0) { fail("compareTo not reflexive", xb, xb, x, x); } if (!xb.Equals(xb)) { fail("equals not reflexive", xb, xb, x, x); } foreach (char y in VALUES) { CharBuffer yb = CharBuffer.Wrap(new char[] { y }); if (xb.CompareTo(yb) != -yb.CompareTo(xb)) { fail("compareTo not anti-symmetric", xb, yb, x, y); } if ((xb.CompareTo(yb) == 0) != xb.Equals(yb)) { fail("compareTo inconsistent with equals", xb, yb, x, y); } if (xb.CompareTo(yb) != x.CompareTo(y)) { fail("Incorrect results for CharBuffer.compareTo", xb, yb, x, y); } if (xb.Equals(yb) != ((x == y) /*|| ((x != x) && (y != y))*/)) { fail("Incorrect results for CharBuffer.equals", xb, yb, x, y); } } } // Sub, dup relPut(b); relGet(b.Duplicate()); b.Position = (13); relGet(b.Duplicate(), 13); relGet(b.Duplicate().Slice(), 13); relGet(b.Slice(), 13); relGet(b.Slice().Duplicate(), 13); // Slice b.Position = (5); CharBuffer sb = b.Slice(); checkSlice(b, sb); b.Position = (0); CharBuffer sb2 = sb.Slice(); checkSlice(sb, sb2); if (!sb.Equals(sb2)) { fail("Sliced slices do not match", sb, sb2); } if ((sb.HasArray) && (sb.ArrayOffset != sb2.ArrayOffset)) { fail("Array offsets do not match: " + sb.ArrayOffset + " != " + sb2.ArrayOffset, sb, sb2); } // Read-only views b.Rewind(); CharBuffer rb = b.AsReadOnlyBuffer(); if (!b.Equals(rb)) { fail("Buffer not equal to read-only view", b, rb); } Show(level + 1, rb); tryCatch(b, typeof(ReadOnlyBufferException), () => { relPut(rb); }); tryCatch(b, typeof(ReadOnlyBufferException), () => { absPut(rb); }); tryCatch(b, typeof(ReadOnlyBufferException), () => { bulkPutArray(rb); }); tryCatch(b, typeof(ReadOnlyBufferException), () => { bulkPutBuffer(rb); }); tryCatch(b, typeof(ReadOnlyBufferException), () => { rb.Compact(); }); if (rb.GetType().Name.StartsWith("Heap", StringComparison.Ordinal)) { tryCatch(b, typeof(ReadOnlyBufferException), () => { var _ = rb.Array; }); tryCatch(b, typeof(ReadOnlyBufferException), () => { var _ = rb.ArrayOffset; }); if (rb.HasArray) { fail("Read-only heap buffer's backing array is accessible", rb); } } // Bulk puts from read-only buffers b.Clear(); rb.Rewind(); b.Put(rb); relPut(b); // Required by testViews }