获取动态属性长度

@param bean
@param charset
@return
@throws IllegalAccessException
@throws InvocationTargetException
@throws NoSuchMethodException
771676
@SuppressWarnings("unchecked")
    public int getDynamicFieldByteSize(Object bean, Charset charset)
            throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
        Object value = PropertyUtils.getProperty(bean, field.getName());
        if (null == value) {
            return 0;
        }
        switch (dynamicFieldType) {
            // 如果是打包剩余的所有Byte
            case allRestByte:
                return ((String) value).getBytes(charset).length;
            // 如果是文件metadata
            case metadata:
                return MetadataMapper.toByte((Set<MetaData>) value, charset).length;
            default:
                return getFieldSize(field);
        }
    }
987092
public static void copyPropertiesToMap(Object bean, String prefix, Map<String, Object> map) {
        Class clazz = bean.getClass();
        Method[] methods = bean.getClass().getMethods();
        for (Method method : methods) {
            // 复制属性
            Class returnc = method.getReturnType();
            if (ReflectUtils.isBeanPropertyReadMethod(method)) {
                String propertyName = ReflectUtils.getPropertyNameFromBeanReadMethod(method);
                try {
                    if (ReflectUtils.getPropertySetterMethod(clazz, propertyName, returnc) == null) {
                        continue; // 还需要有set方法
                    }
                } catch (Exception e) {
                    continue;
                }
                Object val;
                try {
                    val = method.invoke(bean);
                } catch (InvocationTargetException e) {
                    throw new SofaRpcRuntimeException("Can't access copy " + propertyName, e.getCause());
                } catch (IllegalAccessException e) {
                    throw new SofaRpcRuntimeException("Can't access copy " + propertyName, e);
                }
                if (val != null) { // 值不为空，放入缓存
                    map.put(prefix + propertyName, val);
                }
            }
        }
        Field[] fields = bean.getClass().getFields();
        for (Field field : fields) {
            String fieldName = field.getName();
            if (map.containsKey(prefix + fieldName)) {
                continue;
            }
            int m = field.getModifiers();
            if (!Modifier.isStatic(m) && !Modifier.isTransient(m)) {
                Object val = null;
                try {
                    if (field.isAccessible()) {
                        val = field.get(bean);
                    } else {
                        try {
                            field.setAccessible(true);
                            val = field.get(bean);
                        } finally {
                            field.setAccessible(false);
                        }
                    }
                } catch (IllegalAccessException e) {
                    // LOGGER.warn("Can't access field" + fieldName + "when copy value to context", e);
                }
                if (val != null) {
                    map.put(prefix + fieldName, val);
                }
            }
        }
    }
308898
public void parseRequestStream(InputStream inputStream, String charset) throws IOException {
		setLoaded();

		MultipartRequestInputStream input = new MultipartRequestInputStream(inputStream);
		input.readBoundary();
		while (true) {
			UploadFileHeader header = input.readDataHeader(charset);
			if (header == null) {
				break;
			}

			if (header.isFile == true) {
				// 文件类型的表单项
				String fileName = header.fileName;
				if (fileName.length() > 0 && header.contentType.contains("application/x-macbinary")) {
					input.skipBytes(128);
				}
				UploadFile newFile = new UploadFile(header, setting);
				newFile.processStream(input);

				putFile(header.formFieldName, newFile);
			} else {
				// 标准表单项
				ByteArrayOutputStream fbos = new ByteArrayOutputStream(1024);
				input.copy(fbos);
				String value = (charset != null) ? new String(fbos.toByteArray(), charset) : new String(fbos.toByteArray());
				putParameter(header.formFieldName, value);
			}

			input.skipBytes(1);
			input.mark(1);

			// read byte, but may be end of stream
			int nextByte = input.read();
			if (nextByte == -1 || nextByte == '-') {
				input.reset();
				break;
			}
			input.reset();
		}
	}
967058
public int getFieldsSendTotalByteSize(Object bean, Charset charset) {
        if (!hasDynamicField()) {
            return fieldsTotalSize;
        } else {
            return getDynamicTotalFieldSize(bean, charset);
        }
    }
913967
@Nonnull
  public String getAsString (@Nonnull final Charset aCharset)
  {
    return new String (m_aBuffer.array (), m_aBuffer.arrayOffset (), m_aBuffer.position (), aCharset);
  }
1364518
@Nullable
  public static Charset determineXMLCharset (@Nonnull final byte [] aBytes)
  {
    ValueEnforcer.notNull (aBytes, "Bytes");

    Charset aParseCharset = null;
    int nSearchOfs = 0;

    if (aBytes.length > 0)
    {
      // Check if a BOM is present
      // Read at maximum 4 bytes (max BOM bytes)
      try (
          NonBlockingByteArrayInputStream aIS = new NonBlockingByteArrayInputStream (aBytes,
                                                                                     0,
                                                                                     Math.min (EUnicodeBOM.getMaximumByteCount (),
                                                                                               aBytes.length)))
      {
        // Check for BOM first
        final InputStreamAndCharset aISC = CharsetHelper.getInputStreamAndCharsetFromBOM (aIS);
        if (aISC.hasBOM ())
        {
          // A BOM was found, but not necessarily a charset could uniquely be
          // identified - skip the
          // BOM bytes and continue determination from there
          nSearchOfs = aISC.getBOM ().getByteCount ();
        }

        if (aISC.hasCharset ())
        {
          // A BOM was found, and that BOM also has a unique charset assigned
          aParseCharset = aISC.getCharset ();
        }
      }
    }

    // No charset found and enough bytes left?
    if (aParseCharset == null && aBytes.length - nSearchOfs >= 4)
      if (_match (aBytes, nSearchOfs, CS_UTF32_BE))
        aParseCharset = CHARSET_UTF_32BE;
      else
        if (_match (aBytes, nSearchOfs, CS_UTF32_LE))
          aParseCharset = CHARSET_UTF_32LE;
        else
          if (_match (aBytes, nSearchOfs, CS_UTF16_BE))
            aParseCharset = StandardCharsets.UTF_16BE;
          else
            if (_match (aBytes, nSearchOfs, CS_UTF16_LE))
              aParseCharset = StandardCharsets.UTF_16LE;
            else
              if (_match (aBytes, nSearchOfs, CS_UTF8))
                aParseCharset = StandardCharsets.UTF_8;
              else
                if (_match (aBytes, nSearchOfs, CS_EBCDIC))
                  aParseCharset = CHARSET_EBCDIC;
                else
                  if (_match (aBytes, nSearchOfs, CS_IBM290))
                    aParseCharset = CHARSET_IBM290;

    if (aParseCharset == null)
    {
      // Fallback charset is always UTF-8
      aParseCharset = FALLBACK_CHARSET;
    }

    // Now read with a reader
    return _parseXMLEncoding (aBytes, nSearchOfs, aParseCharset);
  }
568654
public static String string(ByteBuffer buffer, Charset charset) throws CharacterCodingException
	    {
	        return charset.newDecoder().decode(buffer.duplicate()).toString();
	    }
835736
@Override
    @SuppressWarnings("unchecked")
    public P get(Bean bean) {
        try {
            return (P) getter.apply(bean);
        } catch (RuntimeException | Error ex) {
            throw ex;
        } catch (Throwable ex) {
            throw new RuntimeException(ex);
        }
    }
1356687
public final void setCharsetName(String charsetName) {
		if (!Charset.isSupported(charsetName))
			throw new IllegalArgumentException(
					"The specified charset [" + charsetName + "] is not supported by the jvm.");
		this.charsetName = charsetName;
	}
1082280
public static Map<String, String> toMap(byte[] xmlBytes, String charset) throws Exception {
        SAXReader reader = new SAXReader(false);
        reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl",true);
        InputSource source = new InputSource(new ByteArrayInputStream(xmlBytes));
        source.setEncoding(charset);
        Document doc = reader.read(source);
        Map<String, String> params = XmlUtils.toMap(doc.getRootElement());
        return params;
    }
635872
public Object parse(byte [] bytes, String charset) {
        if (bytes == null) {
            throw new IllegalArgumentException("bytes must not be null");
        }

        if (charset == null) {
            throw new IllegalArgumentException("charset must not be null");
        }

        Object content;
        content = createParser().parse(bytes, charset);
        return content;
    }
872179
public boolean tryDecode(byte[] bytes, @Nullable Charset charset) {
    if (charset == null) {
      return false;
    }
    CharsetDecoder decoder = charset.newDecoder()
      .onMalformedInput(CodingErrorAction.REPORT)
      .onUnmappableCharacter(CodingErrorAction.REPORT);

    try {
      decoder.decode(ByteBuffer.wrap(bytes));
    } catch (CharacterCodingException e) {
      return false;
    }
    return true;
  }
965134
public static void setCharsetEncoding(String charset) {
		try {

			String test = "123456890";
			test.getBytes(charset);
			CHARSET_ENCODING = charset;
			encode = true;
		} catch (UnsupportedEncodingException ex) {
			// Reset the encoding to default
			CHARSET_ENCODING = "";
			encode = false;
		}
	}
211755
public static String read(InputStream in, String charset) throws IOException {
        log.debug("开始从流中读取内容");
        charset = charset == null ? "UTF8" : charset;
        int bufSize = 256;
        log.debug("文本编码为：{}，缓冲区大小为{}byte", charset, bufSize);
        return new String(read(in, bufSize), charset);
    }
989952
@Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {

        // 为了做兼容
        DisconfCenterHostFilesStore.getInstance().addJustHostFileSet(fileList);

        List<String> scanPackList = StringUtil.parseStringToStringList(scanPackage, SCAN_SPLIT_TOKEN);
        // unique
        Set<String> hs = new HashSet<String>();
        hs.addAll(scanPackList);
        scanPackList.clear();
        scanPackList.addAll(hs);

        // 进行扫描
        DisconfMgr.getInstance().setApplicationContext(applicationContext);
        DisconfMgr.getInstance().firstScan(scanPackList);

        // register java bean
        registerAspect(registry);
    }
630927
@SuppressWarnings("unchecked")
	public <T> T get(String fieldName) throws BeanException{
		if(Map.class.isAssignableFrom(beanClass)){
			return (T) ((Map<?, ?>)bean).get(fieldName);
		}else{
			try {
				final Method method = BeanUtil.getBeanDesc(beanClass).getGetter(fieldName);
				if(null == method){
					throw new BeanException("No get method for {}", fieldName);
				}
				return (T) method.invoke(this.bean);
			} catch (Exception e) {
				throw new BeanException(e);
			}
		}
	}
615347
public void setCharset(String charset) {
	if (StringUtil.isEmpty(charset)) return;
	this.charset = CharsetUtil.toCharSet(charset.trim());
    }