/QingTingFanBianYi/src/com/alibaba/fastjson/serializer/SerialWriterStringEncoder.java

https://gitlab.com/qt-prometheus/qt-prometheus · Java · 72 lines · 59 code · 9 blank · 4 comment · 4 complexity · 2eceb603b68d95239d76ea8a9336348b MD5 · raw file

  1. package com.alibaba.fastjson.serializer;
  2. import com.alibaba.fastjson.JSONException;
  3. import com.alibaba.fastjson.util.ThreadLocalCache;
  4. import java.nio.ByteBuffer;
  5. import java.nio.CharBuffer;
  6. import java.nio.charset.CharacterCodingException;
  7. import java.nio.charset.Charset;
  8. import java.nio.charset.CharsetEncoder;
  9. import java.nio.charset.CoderResult;
  10. import java.nio.charset.CodingErrorAction;
  11. public class SerialWriterStringEncoder
  12. {
  13. private final CharsetEncoder encoder;
  14. public SerialWriterStringEncoder(Charset paramCharset)
  15. {
  16. this(paramCharset.newEncoder().onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE));
  17. }
  18. public SerialWriterStringEncoder(CharsetEncoder paramCharsetEncoder)
  19. {
  20. this.encoder = paramCharsetEncoder;
  21. }
  22. private static int scale(int paramInt, float paramFloat)
  23. {
  24. return (int)(paramInt * paramFloat);
  25. }
  26. public byte[] encode(char[] paramArrayOfChar, int paramInt1, int paramInt2)
  27. {
  28. if (paramInt2 == 0)
  29. return new byte[0];
  30. this.encoder.reset();
  31. return encode(paramArrayOfChar, paramInt1, paramInt2, ThreadLocalCache.getBytes(scale(paramInt2, this.encoder.maxBytesPerChar())));
  32. }
  33. public byte[] encode(char[] paramArrayOfChar, int paramInt1, int paramInt2, byte[] paramArrayOfByte)
  34. {
  35. ByteBuffer localByteBuffer = ByteBuffer.wrap(paramArrayOfByte);
  36. paramArrayOfChar = CharBuffer.wrap(paramArrayOfChar, paramInt1, paramInt2);
  37. try
  38. {
  39. paramArrayOfChar = this.encoder.encode(paramArrayOfChar, localByteBuffer, true);
  40. if (!paramArrayOfChar.isUnderflow())
  41. paramArrayOfChar.throwException();
  42. paramArrayOfChar = this.encoder.flush(localByteBuffer);
  43. if (!paramArrayOfChar.isUnderflow())
  44. paramArrayOfChar.throwException();
  45. paramInt1 = localByteBuffer.position();
  46. paramArrayOfChar = new byte[paramInt1];
  47. System.arraycopy(paramArrayOfByte, 0, paramArrayOfChar, 0, paramInt1);
  48. return paramArrayOfChar;
  49. }
  50. catch (CharacterCodingException paramArrayOfChar)
  51. {
  52. }
  53. throw new JSONException(paramArrayOfChar.getMessage(), paramArrayOfChar);
  54. }
  55. public CharsetEncoder getEncoder()
  56. {
  57. return this.encoder;
  58. }
  59. }
  60. /* Location: C:\Users\User\dex2jar-2.0\dex\qting\classes-dex2jar.jar
  61. * Qualified Name: com.alibaba.fastjson.serializer.SerialWriterStringEncoder
  62. * JD-Core Version: 0.6.2
  63. */