Exemple #1
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 = (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;
  }
Exemple #2
0
  public boolean setStorageTypeAndCheckProps(
      XStorage xStorage, String sMediaType, boolean bIsRoot, int nMode) {
    boolean bOk = false;

    // get access to the XPropertySet interface
    XPropertySet xPropSet = UnoRuntime.queryInterface(XPropertySet.class, xStorage);
    if (xPropSet != null) {
      try {
        // set "MediaType" property to the stream
        xPropSet.setPropertyValue("MediaType", sMediaType);

        // get "IsRoot" and "OpenMode" properties and control there values
        boolean bPropIsRoot = AnyConverter.toBoolean(xPropSet.getPropertyValue("IsRoot"));
        int nPropMode = AnyConverter.toInt(xPropSet.getPropertyValue("OpenMode"));

        bOk = true;
        if (bPropIsRoot != bIsRoot) {
          Error("'IsRoot' property contains wrong value!");
          bOk = false;
        }

        if ((bIsRoot && (nPropMode | ElementModes.READ) != (nMode | ElementModes.READ))
            || (!bIsRoot && (nPropMode & nMode) != nMode)) {
          Error("'OpenMode' property contains wrong value!");
          bOk = false;
        }
      } catch (Exception e) {
        Error("Can't control properties of substorage, exception: " + e);
      }
    } else {
      Error("Can't get XPropertySet implementation from storage!");
    }

    return bOk;
  }
Exemple #3
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 = (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 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
    try {
      xPropSet.setPropertyValue("UseCommonStoragePasswordEncryption", new Boolean(bEncrypted));
    } catch (Exception e) {
      Error(
          "Can't set 'UseCommonStoragePasswordEncryption' property to substream '"
              + sStreamName
              + "', exception: "
              + e);
      return false;
    }

    if (!WriteBytesToStream(xSubStream, sStreamName, sMediaType, bCompressed, pBytes)) return false;

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

    return true;
  }
Exemple #4
0
  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;
  }
Exemple #5
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);
  }
Exemple #6
0
  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;
  }
Exemple #7
0
  public boolean WBToSubstrOfEncrH(
      XStorage xStorage,
      String sStreamPath,
      String sMediaType,
      boolean bCompressed,
      byte[] pBytes,
      boolean bEncrypted,
      boolean bCommit) {
    // open substream element
    XStream xSubStream = null;
    try {
      XHierarchicalStorageAccess xHStorage =
          (XHierarchicalStorageAccess)
              UnoRuntime.queryInterface(XHierarchicalStorageAccess.class, xStorage);
      if (xHStorage == null) {
        Error("The storage does not support hierarchical access!");
        return false;
      }

      Object oSubStream =
          xHStorage.openStreamElementByHierarchicalName(sStreamPath, ElementModes.WRITE);
      xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't create substream '" + sStreamPath + "'!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't create substream '" + sStreamPath + "', 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 '" + sStreamPath + "'!");
      return false;
    }

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

    if (!WriteBytesToStream(xSubStream, sStreamPath, sMediaType, bCompressed, pBytes)) return false;

    XTransactedObject xTransact =
        (XTransactedObject) UnoRuntime.queryInterface(XTransactedObject.class, xSubStream);
    if (xTransact == null) {
      Error("Substream '" + sStreamPath + "', stream opened for writing must be transacted!");
      return false;
    }

    if (bCommit) {
      try {
        xTransact.commit();
      } catch (Exception e) {
        Error(
            "Can't commit storage after substream '"
                + sStreamPath
                + "' change, exception : "
                + e
                + "!");
        return false;
      }
    }

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

    return true;
  }