Esempio n. 1
0
  /**
   * @param socketChannel
   * @param callbackIndex
   * @param obj
   * @param block
   */
  public static boolean writeByteObject(
      SocketChannel socketChannel, int callbackIndex, Object obj, boolean block) {
    if (socketChannel == null) {
      return false;
    }

    if (obj != null) {
      try {
        byte[] bytes =
            obj.getClass() == String.class
                ? ((String) obj).getBytes(ContextUtils.getCharset())
                : ServerResolverBody.ME.getObjectMapper().writeValueAsBytes(obj);
        if (block) {
          return writeByteBufferBlock(socketChannel, callbackIndex, bytes);

        } else {
          writeByteBuffer(socketChannel, callbackIndex, bytes);
        }

      } catch (Throwable e) {
        if (BeanFactoryUtils.getEnvironment().compareTo(Environment.DEBUG) <= 0) {
          e.printStackTrace();
        }

        LOGGER.error("", e);
        return false;
      }
    }

    return true;
  }
Esempio n. 2
0
  /**
   * @param socketChannel
   * @param callbackIndex
   * @param bytes
   * @return
   */
  public static boolean writeByteBufferBlock(
      SocketChannel socketChannel, int callbackIndex, byte[] bytes) {
    try {
      ByteArrayOutputStream outStream = new ByteArrayOutputStream();
      HelperServer.zipCompress(bytes, 0, bytes.length, outStream);
      InputSocketImpl.writeByteBuffer(socketChannel, callbackIndex, outStream.toByteArray());
      return true;

    } catch (Throwable e) {
      if (BeanFactoryUtils.getEnvironment().compareTo(Environment.DEBUG) <= 0) {
        e.printStackTrace();
      }

      LOGGER.error("", e);
    }

    return false;
  }