private static void writePrimitiveUTF8()

in linkis-engineconn-plugins/hive/src/main/java/org/apache/linkis/engineplugin/hive/serde/CustomerDelimitedJSONSerDe.java [216:375]


  private static void writePrimitiveUTF8(
      OutputStream out,
      Object o,
      PrimitiveObjectInspector oi,
      boolean escaped,
      byte escapeChar,
      boolean[] needsEscape)
      throws IOException {

    PrimitiveObjectInspector.PrimitiveCategory category = oi.getPrimitiveCategory();
    byte[] binaryData = null;
    WritableComparable wc = null;
    switch (category) {
      case BOOLEAN:
        {
          boolean b = ((BooleanObjectInspector) oi).get(o);
          if (b) {
            binaryData = Base64.encodeBase64(trueBytes);
          } else {
            binaryData = Base64.encodeBase64(falseBytes);
          }
          break;
        }
      case BYTE:
        {
          binaryData =
              Base64.encodeBase64(String.valueOf(((ByteObjectInspector) oi).get(o)).getBytes());
          break;
        }
      case SHORT:
        {
          binaryData =
              Base64.encodeBase64(String.valueOf(((ShortObjectInspector) oi).get(o)).getBytes());
          break;
        }
      case INT:
        {
          binaryData =
              Base64.encodeBase64(String.valueOf(((IntObjectInspector) oi).get(o)).getBytes());
          break;
        }
      case LONG:
        {
          binaryData =
              Base64.encodeBase64(String.valueOf(((LongObjectInspector) oi).get(o)).getBytes());
          break;
        }
      case FLOAT:
        {
          binaryData =
              Base64.encodeBase64(String.valueOf(((FloatObjectInspector) oi).get(o)).getBytes());
          break;
        }
      case DOUBLE:
        {
          binaryData =
              Base64.encodeBase64(String.valueOf(((DoubleObjectInspector) oi).get(o)).getBytes());
          break;
        }
      case STRING:
        {
          binaryData =
              Base64.encodeBase64(
                  ((StringObjectInspector) oi).getPrimitiveWritableObject(o).getBytes());
          break;
        }
      case CHAR:
        {
          HiveCharWritable hc = ((HiveCharObjectInspector) oi).getPrimitiveWritableObject(o);
          binaryData = Base64.encodeBase64(String.valueOf(hc).getBytes());
          break;
        }
      case VARCHAR:
        {
          HiveVarcharWritable hc = ((HiveVarcharObjectInspector) oi).getPrimitiveWritableObject(o);
          binaryData = Base64.encodeBase64(String.valueOf(hc).getBytes());
          break;
        }
      case BINARY:
        {
          BytesWritable bw = ((BinaryObjectInspector) oi).getPrimitiveWritableObject(o);
          byte[] toEncode = new byte[bw.getLength()];
          System.arraycopy(bw.getBytes(), 0, toEncode, 0, bw.getLength());
          byte[] toWrite = Base64.encodeBase64(toEncode);
          out.write(toWrite, 0, toWrite.length);
          break;
        }
      case DATE:
        {
          wc = ((DateObjectInspector) oi).getPrimitiveWritableObject(o);
          binaryData = Base64.encodeBase64(String.valueOf(wc).getBytes());
          break;
        }
      case TIMESTAMP:
        {
          wc = ((TimestampObjectInspector) oi).getPrimitiveWritableObject(o);
          binaryData = Base64.encodeBase64(String.valueOf(wc).getBytes());
          break;
        }
      case DECIMAL:
        {
          HiveDecimalObjectInspector decimalOI = (HiveDecimalObjectInspector) oi;
          binaryData = Base64.encodeBase64(String.valueOf(decimalOI).getBytes());
          break;
        }
      default:
        {
          if (!"INTERVAL_YEAR_MONTH".equals(category.name())
              && !"INTERVAL_DAY_TIME".equals(category.name())) {
            throw new RuntimeException("Unknown primitive type: " + category);
          }
          boolean containsIntervalYearMonth = false;
          boolean containsIntervalDayTime = false;
          for (PrimitiveObjectInspector.PrimitiveCategory primitiveCategory :
              PrimitiveObjectInspector.PrimitiveCategory.values()) {
            containsIntervalYearMonth =
                "INTERVAL_YEAR_MONTH".equals(primitiveCategory.name())
                    && "INTERVAL_YEAR_MONTH".equals(category.name());
            containsIntervalDayTime =
                "INTERVAL_DAY_TIME".equals(primitiveCategory.name())
                    && "INTERVAL_DAY_TIME".equals(category.name());
            try {
              if (containsIntervalYearMonth) {
                wc =
                    (WritableComparable)
                        ClassUtils.getClassInstance(
                                "org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveIntervalYearMonthObjectInspector")
                            .getClass()
                            .getMethod("getPrimitiveWritableObject", Object.class)
                            .invoke(oi, o);
                binaryData = Base64.encodeBase64(String.valueOf(wc).getBytes());
                break;
              }
              if (containsIntervalDayTime) {
                wc =
                    (WritableComparable)
                        ClassUtils.getClassInstance(
                                "org.apache.hadoop.hive.serde2.objectinspector.primitive.HiveIntervalDayTimeObjectInspector")
                            .getClass()
                            .getMethod("getPrimitiveWritableObject", Object.class)
                            .invoke(oi, o);
                binaryData = Base64.encodeBase64(String.valueOf(wc).getBytes());
                break;
              }
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
              LOG.error("Fail to invoke method:[getPrimitiveWritableObject]!", e);
            }
          }
          if (containsIntervalYearMonth || containsIntervalDayTime) {
            break;
          } else {
            throw new RuntimeException("Unknown primitive type: " + category);
          }
        }
    }
    if (binaryData == null) {
      throw new RuntimeException("get primitive type is null: " + category);
    }
    out.write(binaryData, 0, binaryData.length);
  }