将Java InputStream作为文件参数发送到Linux命令

问题描述

最近,我参与了Storlet项目,该项目是OpenStack Swift项目的中间件。我不想谈论Storlet,但是总之,Storlet在存储到swift对象存储中的对象(文件)上运行Java代码。 storlet读取的文件以InputSream的形式发送到Java应用程序,这意味着我们不直接访问文件

这是storlet的示例代码,该代码将图像作为输入流并对其进行缩略图化。

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.io.InputStream;
import java.io.OutputStream;

import org.openstack.storlet.common.IStorlet;
import org.openstack.storlet.common.StorletException;
import org.openstack.storlet.common.StorletInputStream;
import org.openstack.storlet.common.StorletLogger;
import org.openstack.storlet.common.StorletobjectOutputStream;
import org.openstack.storlet.common.StorletContainerHandle;
import org.openstack.storlet.common.StorletoutputStream;
import org.openstack.storlet.common.StorletUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.Image;
import java.awt.Transparency;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

public class ThumbnailStorlet implements IStorlet {
   @Override
   public void invoke(ArrayList<StorletInputStream> inputStreams,ArrayList<StorletoutputStream> outputStreams,Map<String,String> parameters,StorletLogger log)
        throws StorletException {
    log.emitLog("ThumbnailStorlet Invoked");

    /*
     * Get input stuff
     */
    HashMap<String,String> object_md;
    StorletInputStream storletInputStream = inputStreams.get(0);
            InputStream thumbnailInputStream = storletInputStream.getStream();
    object_md = storletInputStream.getMetadata();
    /*
     * Get output stuff
     */

    StorletobjectOutputStream storletobjectOutputStream = (StorletobjectOutputStream)outputStreams.get(0);
            OutputStream thumbnailOutputStream = storletobjectOutputStream.getStream();

    /*
     * Set the output Metadata
     */
    log.emitLog("Setting Metadata");
    storletobjectOutputStream.setMetadata(object_md);

    /*
     * Read Input to BufferedImage
     */
    log.emitLog("Reading Input");
    BufferedImage img = null;
            try {
            img = ImageIO.read(thumbnailInputStream);
    } catch (Exception e) {
        log.emitLog("Failed to read input stream to buffered image");
        throw new StorletException("Failed to read input stream to buffered image " + e.getMessage());
    } finally {
        try {
            thumbnailInputStream.close();
        } catch (IOException e) {
            log.emitLog("Failed to close input stream");
        }
    }
    try {
        thumbnailInputStream.close();
    } catch (IOException e) {
        log.emitLog("Failed to close input stream");
    }

    /*
     * Convert
     */
    log.emitLog("Converting");
    int newH = img.getHeight()/8;
    int newW = img.getWidth()/8;
            int type = img.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
    BufferedImage thumbnailImage = new BufferedImage(newW,newH,type);
            Graphics2D g = thumbnailImage.createGraphics();
            g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g.drawImage(img,newW,null);
            g.dispose();

    /*
     * Write
     */
    log.emitLog("Writing Output");
    try {
        ImageIO.write(thumbnailImage,"PNG",thumbnailOutputStream);
    } catch (Exception e) {
        log.emitLog("Failed to write image to out stream");
        throw new StorletException("Failed to write image to out stream " + e.getMessage());
    } finally {
        try {
            thumbnailOutputStream.close();
        } catch (IOException e) {
        }
    }

    try {
            thumbnailOutputStream.close();
    } catch (IOException e) {
    }

    log.emitLog("Done");

}

}

现在.. 我想在Java代码中使用一些运行在GDAL之类的图像上的外部应用程序,假设代码类似于上面的代码(与上面的代码做的并不完全相同)。 GDLA有一些cli命令。例如,该命令

 gdal_translate -of JPEG -co QUALITY=50  input.tif output.jpg

input.tif已经存储到我的对象存储中,并且storlet可以读取它并将其作为输入流提供给我,我也有一些练习如何使用java ProcessBuilder在java内部运行外部进程,但是,想象一下我接收input.tif作为InputStream而不是文件

接下来,我不想将InputStream写回本地存储,因为存储不足(可能对象很大,更多的2GB),并且性能下降。我的应用程序在那里运行。

java中是否有任何方法可以将InputStream作为文件参数传递给外部进程,而无需将其存储在磁盘上。

我正在Ubuntu Docker上运行代码

解决方法

我认为您不能这样做,File需要存储在File system(本地或远程)中才能读取。您可以尝试将ByteArrayInputStream建立在阅读过程的基础上,但GDAL过程应支持该类型的输入:

https://gdal.org/programs/gdal_translate.html#cmdoption-gdal_translate-arg-src_dataset

根据GDAL文档,似乎不可能:

<src_dataset>
The source dataset name. It can be either file name,URL of data source or subdataset name for multi-dataset files.