Hisoka-X commented on code in PR #6826: URL: https://github.com/apache/seatunnel/pull/6826#discussion_r1609933293
########## seatunnel-connectors-v2/connector-file/connector-file-base/src/main/java/org/apache/seatunnel/connectors/seatunnel/file/source/reader/BinaryReadStrategy.java: ########## @@ -0,0 +1,100 @@ +/* + * 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.seatunnel.connectors.seatunnel.file.source.reader; + +import org.apache.seatunnel.api.source.Collector; +import org.apache.seatunnel.api.table.type.BasicType; +import org.apache.seatunnel.api.table.type.PrimitiveByteArrayType; +import org.apache.seatunnel.api.table.type.SeaTunnelDataType; +import org.apache.seatunnel.api.table.type.SeaTunnelRow; +import org.apache.seatunnel.api.table.type.SeaTunnelRowType; +import org.apache.seatunnel.connectors.seatunnel.file.config.BaseSourceConfigOptions; +import org.apache.seatunnel.connectors.seatunnel.file.config.HadoopConf; +import org.apache.seatunnel.connectors.seatunnel.file.exception.FileConnectorException; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Arrays; + +/** Used to read file to binary stream */ +public class BinaryReadStrategy extends AbstractReadStrategy { + + public static SeaTunnelRowType binaryRowType = + new SeaTunnelRowType( + new String[] {"data", "relativePath", "partIndex"}, + new SeaTunnelDataType[] { + PrimitiveByteArrayType.INSTANCE, BasicType.STRING_TYPE, BasicType.LONG_TYPE + }); + + private File basePath; + + @Override + public void init(HadoopConf conf) { + super.init(conf); + basePath = new File(pluginConfig.getString(BaseSourceConfigOptions.FILE_PATH.key())); + } + + @Override + public void read(String path, String tableId, Collector<SeaTunnelRow> output) + throws IOException, FileConnectorException { + try (InputStream inputStream = hadoopFileSystemProxy.getInputStream(path)) { + String relativePath; + if (basePath.isFile()) { + relativePath = basePath.getName(); + } else { + relativePath = + path.substring( + path.indexOf(basePath.getAbsolutePath()) + + basePath.getAbsolutePath().length()); + if (relativePath.startsWith(File.separator)) { + relativePath = relativePath.substring(File.separator.length()); + } + } + // TODO config this size + int maxSize = 1024; + byte[] buffer = new byte[maxSize]; + long partIndex = 0; + int readSize; + while ((readSize = inputStream.read(buffer)) != -1) { + if (readSize != maxSize) { + buffer = Arrays.copyOf(buffer, readSize); + } + SeaTunnelRow row = new SeaTunnelRow(new Object[] {buffer, relativePath, partIndex}); + buffer = new byte[1024]; Review Comment: Thanks for the suggestion, the original version is actually exactly as you described. But `buffer` is an object and will eventually be used by the sink writer. If we reuse it in advance, the sink will not be able to get the correct data. -- 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]
