Ejemplo n.º 1
0
  public boolean WriteBytesToEncrStreamH(
      XStorage xStorage,
      String sStreamPath,
      String sMediaType,
      boolean bCompressed,
      byte[] pBytes,
      String sPass,
      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.openEncryptedStreamElementByHierarchicalName(
              sStreamPath, ElementModes.WRITE, sPass);
      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;
    }

    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;
  }
Ejemplo n.º 2
0
  public boolean commitStorage(XStorage xStorage) {
    // XTransactedObject must be supported by storages
    XTransactedObject xTransact = UnoRuntime.queryInterface(XTransactedObject.class, xStorage);
    if (xTransact == null) {
      Error("Storage doesn't implement transacted access!");
      return false;
    }

    try {
      xTransact.commit();
    } catch (Exception e) {
      Error("Storage commit failed, exception:" + e);
      return false;
    }

    return true;
  }
Ejemplo n.º 3
0
  public int ChangeStreamPassH(
      XStorage xStorage, String sPath, String sOldPass, String sNewPass, boolean bCommit) {
    // open substream element
    XHierarchicalStorageAccess xHStorage =
        (XHierarchicalStorageAccess)
            UnoRuntime.queryInterface(XHierarchicalStorageAccess.class, xStorage);
    if (xHStorage == null) {
      Error("The storage does not support hierarchical access!");
      return 0;
    }

    XStream xSubStream = null;
    try {
      Object oSubStream =
          xHStorage.openEncryptedStreamElementByHierarchicalName(
              sPath, ElementModes.WRITE, sOldPass);
      xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't open encrypted substream '" + sPath + "'!");
        return 0;
      }
    } catch (Exception e) {
      Error("Can't open encrypted substream '" + sPath + "', exception : " + e + "!");
      return 0;
    }

    // change the password for the stream
    XEncryptionProtectedSource xStreamEncryption =
        (XEncryptionProtectedSource)
            UnoRuntime.queryInterface(XEncryptionProtectedSource.class, xSubStream);

    if (xStreamEncryption == null) {
      Message(
          "Optional interface XEncryptionProtectedSource is not implemented, feature can not be tested!");
      return -1;
    }

    try {
      xStreamEncryption.setEncryptionPassword(sNewPass);
    } catch (Exception e) {
      Error("Can't change encryption key of the substream '" + sPath + "', exception:" + e);
      return 0;
    }

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

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

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

    return 1;
  }
Ejemplo n.º 4
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;
  }
Ejemplo n.º 5
0
  public boolean test() {
    try {
      byte[] pBytes0 = new byte[0];
      byte[] pBytes18 = new byte[18000];
      byte[] pBytes36 = new byte[36000];

      for (int nInitInd = 0; nInitInd < 36000; nInitInd++) {
        pBytes36[nInitInd] = (Integer.valueOf(nInitInd >> ((nInitInd % 2) * 8))).byteValue();
        if (nInitInd < 18000)
          pBytes18[nInitInd] = (Integer.valueOf(256 - pBytes36[nInitInd])).byteValue();
      }

      System.out.println(
          "This test can take up to some hours. The file size currently is about 50000.");
      System.out.println("Progress: ");
      for (int nAvailableBytes = nMinTestLen; nAvailableBytes < nMaxTestLen; nAvailableBytes++) {
        Object oBStream = new BorderedStream(nAvailableBytes);
        XStream xBorderedStream = (XStream) UnoRuntime.queryInterface(XStream.class, oBStream);
        if (xBorderedStream == null) {
          m_aTestHelper.Error("Can't create bordered stream!");
          return false;
        }

        // create storage based on the temporary stream
        Object pArgs[] = new Object[2];
        pArgs[0] = (Object) xBorderedStream;
        pArgs[1] = Integer.valueOf(ElementModes.WRITE);

        Object oTempStorage = m_xStorageFactory.createInstanceWithArguments(pArgs);
        XStorage xTempStorage = (XStorage) UnoRuntime.queryInterface(XStorage.class, oTempStorage);
        if (xTempStorage == null) {
          m_aTestHelper.Error("Can't create temporary storage representation!");
          return false;
        }

        XTransactedObject xTransact =
            (XTransactedObject) UnoRuntime.queryInterface(XTransactedObject.class, xTempStorage);
        if (xTransact == null) {
          m_aTestHelper.Error("This test is designed for storages in transacted mode!");
          return false;
        }

        if (!m_aTestHelper.WriteBytesToSubstream(
            xTempStorage, "SubStream" + 0, "MediaType1", true, pBytes0)) return false;
        if (!m_aTestHelper.WriteBytesToSubstream(
            xTempStorage, "SubStream" + 18, "MediaType2", true, pBytes18)) return false;
        if (!m_aTestHelper.WriteBytesToSubstream(
            xTempStorage, "SubStream" + 36, "MediaType3", true, pBytes36)) return false;

        if (nAvailableBytes > 0 && nAvailableBytes % 100 == 0)
          System.out.println(" " + nAvailableBytes);

        if (nAvailableBytes > 0 && nAvailableBytes % 2 == 1) System.out.print("#");

        try {
          xTransact.commit();

          System.out.println("");
          if (!m_aTestHelper.disposeStorage(xTempStorage)) return false;

          // SUCCESS
          return true;
        } catch (UseBackupException aExc) {
          // when there is not enough place in the target location and the target file is empty
          // the direct writing will fail and must throw this exception with empty URL
          if (aExc.TemporaryFileURL.length() != 0) return false;
        } catch (Exception e) {
          System.out.println("");
          m_aTestHelper.Error(
              "Unexpected exception: " + e + "\nnAvailableBytes = " + nAvailableBytes);
          return false;
        }
      }

      return false;
    } catch (Exception e) {
      m_aTestHelper.Error("Exception: " + e);
      return false;
    }
  }