Exemplo n.º 1
0
  public boolean WBToSubstrOfEncr(
      XStorage xStorage,
      String sStreamName,
      String sMediaType,
      boolean bCompressed,
      byte[] pBytes,
      boolean bEncrypted) {
    // open substream element
    XStream xSubStream = null;
    try {
      Object oSubStream = xStorage.openStreamElement(sStreamName, ElementModes.WRITE);
      xSubStream = 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 access to the XPropertySet interface
    XPropertySet xPropSet = UnoRuntime.queryInterface(XPropertySet.class, xSubStream);
    if (xPropSet == null) {
      Error("Can't get XPropertySet implementation from substream '" + sStreamName + "'!");
      return false;
    }

    // set properties to the stream
    try {
      xPropSet.setPropertyValue("Encrypted", Boolean.valueOf(bEncrypted));
    } catch (Exception e) {
      Error("Can't set 'Encrypted' property to substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    return WriteBytesToStream(xSubStream, sStreamName, sMediaType, bCompressed, pBytes);
  }
Exemplo n.º 2
0
  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 = 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 = 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", Boolean.valueOf(bCompressed));
    } catch (Exception e) {
      Error("Can't set properties to substream '" + sStreamName + "', exception: " + e);
      return false;
    }

    // check size property of the stream
    try {
      int nSize = AnyConverter.toInt(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
    XComponent xComponent = UnoRuntime.queryInterface(XComponent.class, xStream);
    if (xComponent == null) {
      Error("Can't get XComponent implementation from substream '" + sStreamName + "'!");
      return false;
    }
    xComponent.dispose();

    return true;
  }