コード例 #1
0
ファイル: TestHelper.java プロジェクト: vancuvit/core
  public boolean WriteBytesToStream(
      XStream xStream, String sStreamName, String sMediaType, boolean bCompressed, byte[] pBytes) {
    // get output stream of substream
    XOutputStream xOutput = xStream.getOutputStream();
    if (xOutput == null) {
      Error("Can't get XOutputStream implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // get XTrucate implementation from output stream
    XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface(XTruncate.class, xOutput);
    if (xTruncate == null) {
      Error("Can't get XTruncate implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // write requested byte sequence
    try {
      xTruncate.truncate();
      xOutput.writeBytes(pBytes);
    } catch (Exception e) {
      Error("Can't write to stream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // get access to the XPropertySet interface
    XPropertySet xPropSet = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xStream);
    if (xPropSet == null) {
      Error("Can't get XPropertySet implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // set properties to the stream
    try {
      xPropSet.setPropertyValue("MediaType", sMediaType);
      xPropSet.setPropertyValue("Compressed", new Boolean(bCompressed));
    } catch (Exception e) {
      Error("Can't set properties to substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // check size property of the stream
    try {
      long nSize = AnyConverter.toLong(xPropSet.getPropertyValue("Size"));
      if (nSize != pBytes.length) {
        Error("The 'Size' property of substream '" + sStreamName + "' contains wrong value!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    return true;
  }
コード例 #2
0
ファイル: TestHelper.java プロジェクト: vancuvit/core
  public boolean closeOutput(XStream xStream) {
    XOutputStream xOutTemp = null;
    try {
      xOutTemp = xStream.getOutputStream();
      if (xOutTemp == null) {
        Error("Can't get the output part of a stream!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't get the output part of a stream, exception :" + e);
      return false;
    }

    try {
      xOutTemp.closeOutput();
    } catch (Exception e) {
      Error("Can't close output part of a stream, exception :" + e);
      return false;
    }

    return true;
  }
コード例 #3
0
ファイル: TestHelper.java プロジェクト: vancuvit/core
  public String CreateTempFile(XMultiServiceFactory xMSF) {
    String sResult = null;

    // try to get temporary file representation
    XPropertySet xTempFileProps = null;
    try {
      Object oTempFile = xMSF.createInstance("com.sun.star.io.TempFile");
      xTempFileProps = (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, oTempFile);
    } catch (Exception e) {
    }

    if (xTempFileProps != null) {
      try {
        xTempFileProps.setPropertyValue("RemoveFile", new Boolean(false));
        sResult = AnyConverter.toString(xTempFileProps.getPropertyValue("Uri"));
      } catch (Exception e) {
        Error("Can't control TempFile properties, exception: " + e);
      }
    } else {
      Error("Can't create temporary file representation!");
    }

    // close temporary file explicitly
    try {
      XStream xStream = (XStream) UnoRuntime.queryInterface(XStream.class, xTempFileProps);
      if (xStream != null) {
        XOutputStream xOut = xStream.getOutputStream();
        if (xOut != null) xOut.closeOutput();

        XInputStream xIn = xStream.getInputStream();
        if (xIn != null) xIn.closeInput();
      } else Error("Can't close TempFile!");
    } catch (Exception e) {
      Error("Can't close TempFile, exception: " + e);
    }

    return sResult;
  }
コード例 #4
0
ファイル: TestHelper.java プロジェクト: vancuvit/core
  public boolean WriteBytesToSubstreamDefaultCompressed(
      XStorage xStorage, String sStreamName, String sMediaType, byte[] pBytes) {
    // open substream element
    XStream xSubStream = null;
    try {
      Object oSubStream = xStorage.openStreamElement(sStreamName, ElementModes.WRITE);
      xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't create substream '" + sStreamName + "'!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't create substream '" + sStreamName + "', exception : " + e + "!");
      return false;
    }

    // get output stream of substream
    XOutputStream xOutput = xSubStream.getOutputStream();
    if (xOutput == null) {
      Error("Can't get XOutputStream implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // get XTrucate implementation from output stream
    XTruncate xTruncate = (XTruncate) UnoRuntime.queryInterface(XTruncate.class, xOutput);
    if (xTruncate == null) {
      Error("Can't get XTruncate implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // write requested byte sequence
    try {
      xTruncate.truncate();
      xOutput.writeBytes(pBytes);
    } catch (Exception e) {
      Error("Can't write to stream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // get access to the XPropertySet interface
    XPropertySet xPropSet =
        (XPropertySet) UnoRuntime.queryInterface(XPropertySet.class, xSubStream);
    if (xPropSet == null) {
      Error("Can't get XPropertySet implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // set properties to the stream
    // do not set the compressed property
    try {
      xPropSet.setPropertyValue("MediaType", sMediaType);
    } catch (Exception e) {
      Error("Can't set properties to substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // check size property of the stream
    try {
      long nSize = AnyConverter.toLong(xPropSet.getPropertyValue("Size"));
      if (nSize != pBytes.length) {
        Error("The 'Size' property of substream '" + sStreamName + "' contains wrong value!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't get 'Size' property from substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // free the stream resources, garbage collector may remove the object too late
    if (!disposeStream(xSubStream, sStreamName)) return false;

    return true;
  }