在流中返回多个列?

时间:2013-10-11 09:45:48

标签: java java-ee

我的代码如下:

 public synchronized InputStream getResourceStream(final String name)
        throws ResourceNotFoundException
    {
        if (org.apache.commons.lang.StringUtils.isEmpty(name))
        {
            throw new ResourceNotFoundException("DataSourceResourceLoader: Template name was empty or null");
        }

        Connection conn = null;
        ResultSet rs = null;
        PreparedStatement ps = null;
        try
        {
            conn = openDbConnection();
            ps = getStatement(conn, templateColumn, name);
            rs = ps.executeQuery();

            if (rs.next())
            {
                InputStream stream = rs.getBinaryStream(templateColumn);
                if (stream == null)
                {
                    throw new ResourceNotFoundException("DataSourceResourceLoader: "
                                                        + "template column for '"
                                                        + name + "' is null");
                }

                return new BufferedInputStream(stream);
            }
            else
            {
                throw new ResourceNotFoundException("DataSourceResourceLoader: "
                                                    + "could not find resource '"
                                                    + name + "'");

            }
        }
        catch (SQLException sqle)
        {
            String msg = "DataSourceResourceLoader: database problem while getting resource '"
                         + name + "': ";

            log.error(msg, sqle);
            throw new ResourceNotFoundException(msg);
        }
        catch (NamingException ne)
        {
            String msg = "DataSourceResourceLoader: database problem while getting resource '"
                         + name + "': ";

            log.error(msg, ne);
            throw new ResourceNotFoundException(msg);
        }
        finally
        {
            closeResultSet(rs);
            closeStatement(ps);
            closeDbConnection(conn);
        }
    }

上述方法返回类型为InputStream。在上面我得到列值为InputStream stream = rs.getBinaryStream(templateColumn);并返回相同的值。 现在我的要求是我必须检索另一列的值并返回同一个流和templateColumn。我怎么能这样做?

基本上在上面的逻辑中,我必须再添加一行,如下所示。

InputStream stream2 = rs.getBinaryStream(oneMoreColumn);

现在可以在单个Stream中返回两个值吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

你可以将两个流完全读入byte[]并返回一个新的ByteArrayInputStream,但这没有任何意义,因为客户端代码不会知道数组的哪个部分是你的首先是哪个部分是第二个流。

而是更改您的方法以返回包含两个InputStream字段的对象。

相关问题