internal static sbyte[] Encode(Charset cs, char[] ca, int off, int len) { CharsetEncoder ce = cs.NewEncoder(); int en = Scale(len, ce.MaxBytesPerChar()); sbyte[] ba = new sbyte[en]; if (len == 0) { return(ba); } bool isTrusted = false; if (System.SecurityManager != null) { if (!(isTrusted = (cs.GetType().ClassLoader0 == null))) { ca = Arrays.CopyOfRange(ca, off, off + len); off = 0; } } ce.OnMalformedInput(CodingErrorAction.REPLACE).OnUnmappableCharacter(CodingErrorAction.REPLACE).Reset(); if (ce is ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, off, len, ba); return(SafeTrim(ba, blen, cs, isTrusted)); } else { ByteBuffer bb = ByteBuffer.Wrap(ba); CharBuffer cb = CharBuffer.Wrap(ca, off, len); try { CoderResult cr = ce.Encode(cb, bb, true); if (!cr.Underflow) { cr.ThrowException(); } cr = ce.Flush(bb); if (!cr.Underflow) { cr.ThrowException(); } } catch (CharacterCodingException x) { throw new Error(x); } return(SafeTrim(ba, bb.Position(), cs, isTrusted)); } }
internal sbyte[] GetBytes(String s) { CharsetEncoder ce = Encoder().Reset(); char[] ca = s.ToCharArray(); int len = (int)(ca.Length * ce.MaxBytesPerChar()); sbyte[] ba = new sbyte[len]; if (len == 0) { return(ba); } // UTF-8 only for now. Other ArrayDeocder only handles // CodingErrorAction.REPLACE mode. if (IsUTF8 && ce is ArrayEncoder) { int blen = ((ArrayEncoder)ce).encode(ca, 0, ca.Length, ba); if (blen == -1) // malformed { throw new IllegalArgumentException("MALFORMED"); } return(Arrays.CopyOf(ba, blen)); } ByteBuffer bb = ByteBuffer.Wrap(ba); CharBuffer cb = CharBuffer.Wrap(ca); CoderResult cr = ce.Encode(cb, bb, true); if (!cr.Underflow) { throw new IllegalArgumentException(cr.ToString()); } cr = ce.Flush(bb); if (!cr.Underflow) { throw new IllegalArgumentException(cr.ToString()); } if (bb.Position() == ba.Length) // defensive copy? { return(ba); } else { return(Arrays.CopyOf(ba, bb.Position())); } }
/// <summary> /// Converts the provided String to bytes using the /// UTF-8 encoding. /// </summary> /// <remarks> /// Converts the provided String to bytes using the /// UTF-8 encoding. If <code>replace</code> is true, then /// malformed input is replaced with the /// substitution character, which is U+FFFD. Otherwise the /// method throws a MalformedInputException. /// </remarks> /// <returns> /// ByteBuffer: bytes stores at ByteBuffer.array() /// and length is ByteBuffer.limit() /// </returns> /// <exception cref="CharacterCodingException"/> public static ByteBuffer Encode(string @string, bool replace) { CharsetEncoder encoder = EncoderFactory.Get(); if (replace) { encoder.OnMalformedInput(CodingErrorAction.Replace); encoder.OnUnmappableCharacter(CodingErrorAction.Replace); } ByteBuffer bytes = encoder.Encode(CharBuffer.Wrap(@string.ToCharArray())); if (replace) { encoder.OnMalformedInput(CodingErrorAction.Report); encoder.OnUnmappableCharacter(CodingErrorAction.Report); } return(bytes); }
internal virtual sbyte[] Encode(char[] ca, int off, int len) { int en = Scale(len, Ce.MaxBytesPerChar()); sbyte[] ba = new sbyte[en]; if (len == 0) { return(ba); } if (Ce is ArrayEncoder) { int blen = ((ArrayEncoder)Ce).encode(ca, off, len, ba); return(SafeTrim(ba, blen, Cs, IsTrusted)); } else { Ce.Reset(); ByteBuffer bb = ByteBuffer.Wrap(ba); CharBuffer cb = CharBuffer.Wrap(ca, off, len); try { CoderResult cr = Ce.Encode(cb, bb, true); if (!cr.Underflow) { cr.ThrowException(); } cr = Ce.Flush(bb); if (!cr.Underflow) { cr.ThrowException(); } } catch (CharacterCodingException x) { // Substitution is always enabled, // so this shouldn't happen throw new Error(x); } return(SafeTrim(ba, bb.Position(), Cs, IsTrusted)); } }
internal virtual void EncodeName(CharsetEncoder enc) { ByteBuffer b; try { b = enc.Encode(CharBuffer.Wrap(GetName())); } catch (CharacterCodingException) { // This should so never happen. throw new RuntimeException(MessageFormat.Format(JGitText.Get().unencodeableFile, GetName())); } encodedNameLen = b.Limit(); if (b.HasArray() && b.ArrayOffset() == 0) { encodedName = ((byte[])b.Array()); } else { b.Get(encodedName = new byte[encodedNameLen]); } }