openinx commented on a change in pull request #4221:
URL: https://github.com/apache/iceberg/pull/4221#discussion_r826800148



##########
File path: 
dell/src/main/java/org/apache/iceberg/dell/ecs/PropertiesSerDesUtil.java
##########
@@ -0,0 +1,102 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iceberg.dell.ecs;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.io.UncheckedIOException;
+import java.nio.charset.StandardCharsets;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Convert Map properties to bytes.
+ */
+public class PropertiesSerDesUtil {
+
+  private PropertiesSerDesUtil() {
+  }
+
+  /**
+   * Version of current implementation.
+   */
+  private static final String CURRENT_VERSION = "0";
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(PropertiesSerDesUtil.class);
+
+  /**
+   * Read properties
+   *
+   * @param version is the version of {@link PropertiesSerDesUtil}
+   */
+  public static Map<String, String> read(InputStream input, String version) {
+    Preconditions.checkArgument(version.equals(CURRENT_VERSION),
+        "Properties version is not match", version);
+    Properties jdkProperties = new Properties();
+    try {
+      jdkProperties.load(new InputStreamReader(input, StandardCharsets.UTF_8));
+    } catch (IOException e) {
+      LOG.error("fail to read properties", e);
+      throw new UncheckedIOException(e);
+    }
+
+    Set<String> propertyNames = jdkProperties.stringPropertyNames();
+    Map<String, String> properties = Maps.newHashMap();
+    for (String name : propertyNames) {
+      properties.put(name, jdkProperties.getProperty(name));
+    }
+
+    return Collections.unmodifiableMap(properties);
+  }
+
+  /**
+   * Write properties, the version is {@link #currentVersion()}
+   */
+  public static byte[] toBytes(Map<String, String> value) {
+    Properties jdkProperties = new Properties();
+    for (Map.Entry<String, String> entry : value.entrySet()) {
+      jdkProperties.setProperty(entry.getKey(), entry.getValue());
+    }
+
+    try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
+      jdkProperties.store(new OutputStreamWriter(output, 
StandardCharsets.UTF_8), null);

Review comment:
       Should we also close the `OutputStreamWriter` ? 




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to