int SetCharSequence0(int index, ICharSequence sequence, Encoding encoding, bool expand)
 {
     if (ReferenceEquals(encoding, Encoding.UTF8))
     {
         int length = ByteBufferUtil.Utf8MaxBytes(sequence);
         if (expand)
         {
             this.EnsureWritable0(length);
             this.CheckIndex0(index, length);
         }
         else
         {
             this.CheckIndex(index, length);
         }
         return(ByteBufferUtil.WriteUtf8(this, index, sequence, sequence.Count));
     }
     if (ReferenceEquals(encoding, Encoding.ASCII))
     {
         int length = sequence.Count;
         if (expand)
         {
             this.EnsureWritable0(length);
             this.CheckIndex0(index, length);
         }
         else
         {
             this.CheckIndex(index, length);
         }
         return(ByteBufferUtil.WriteAscii(this, index, sequence, length));
     }
     byte[] bytes = encoding.GetBytes(sequence.ToString());
     if (expand)
     {
         this.EnsureWritable0(bytes.Length);
         // setBytes(...) will take care of checking the indices.
     }
     this.SetBytes(index, bytes);
     return(bytes.Length);
 }