Exemple #1
0
  public boolean WriteBytesToSubstream(
      XStorage xStorage,
      String sStreamName,
      String sMediaType,
      boolean bCompressed,
      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;
    }

    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 #2
0
  public boolean checkStream(
      XStorage xParentStorage,
      String sName,
      String sMediaType,
      boolean bCompressed,
      byte[] pBytes) {
    // open substream element first
    XStream xSubStream = null;
    try {
      Object oSubStream = xParentStorage.openStreamElement(sName, ElementModes.READ);
      xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't open substream '" + sName + "'!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't open substream '" + sName + "', exception : " + e + "!");
      return false;
    }

    boolean bResult = InternalCheckStream(xSubStream, sName, sMediaType, bCompressed, pBytes, true);

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

    return bResult;
  }
Exemple #3
0
  public boolean checkEncrStream(
      XStorage xParentStorage, String sName, String sMediaType, byte[] pBytes, byte[] pPass) {
    // Important: a common password for any of parent storage should not be set or
    //            should be different from pPass

    if (pPass.length == 0) {
      Error("Wrong password is used in the test!");
      return false;
    }

    try {
      xParentStorage.openStreamElement(sName, ElementModes.READ);
      Error("Encrypted stream '" + sName + "' was opened without password!");
      return false;
    } catch (WrongPasswordException wpe) {
    } catch (Exception e) {
      Error(
          "Unexpected exception in case of opening of encrypted stream '"
              + sName
              + "' without password: "******"!");
      return false;
    }

    byte pWrongPass[] = {1, 1};
    pWrongPass[0] += pPass[0];
    try {
      xParentStorage.openEncryptedStreamElement(sName, ElementModes.READ, new String(pWrongPass));
      Error("Encrypted stream '" + sName + "' was opened with wrong password!");
      return false;
    } catch (WrongPasswordException wpe) {
    } catch (Exception e) {
      Error(
          "Unexpected exception in case of opening of encrypted stream '"
              + sName
              + "' with wrong password: "******"!");
      return false;
    }

    XStream xSubStream = null;
    try {
      Object oSubStream =
          xParentStorage.openEncryptedStreamElement(sName, ElementModes.READ, new String(pPass));
      xSubStream = UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't open encrypted substream '" + sName + "'!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't open encrypted substream '" + sName + "', exception : " + e + "!");
      return false;
    }

    return InternalCheckStream(xSubStream, sName, sMediaType, pBytes);
  }
Exemple #4
0
  public boolean cantOpenStream(XStorage xStorage, String sName, int nMode) {
    // try to open the substream with specified mode must fail
    try {
      Object oDummyStream = xStorage.openStreamElement(sName, nMode);
      Error("The trying to open substream '" + sName + "' must fail!");
    } catch (Exception e) {
      return true;
    }

    return false;
  }
Exemple #5
0
  public XStream OpenStream(XStorage xStorage, String sStreamName, int nMode) {
    // open substream element
    XStream xSubStream = null;
    try {
      Object oSubStream = xStorage.openStreamElement(sStreamName, nMode);
      xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) Error("Can't create substream '" + sStreamName + "'!");
    } catch (Exception e) {
      Error("Can't create substream '" + sStreamName + "', exception : " + e + "!");
    }

    return xSubStream;
  }
Exemple #6
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 #7
0
  public boolean checkStream(
      XStorage xParentStorage, String sName, String sMediaType, byte[] pBytes) {
    // open substream element first
    XStream xSubStream = null;
    try {
      Object oSubStream = xParentStorage.openStreamElement(sName, ElementModes.READ);
      xSubStream = UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't open substream '" + sName + "'!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't open substream '" + sName + "', exception : " + e + "!");
      return false;
    }

    return InternalCheckStream(xSubStream, sName, sMediaType, pBytes);
  }
Exemple #8
0
  public boolean WriteBytesToSubstream(
      XStorage xStorage,
      String sStreamName,
      String sMediaType,
      boolean bCompressed,
      byte[] pBytes) {
    // 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;
    }

    return WriteBytesToStream(xSubStream, sStreamName, sMediaType, bCompressed, pBytes);
  }
Exemple #9
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 #10
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 #11
0
  public boolean checkEncrStream(
      XStorage xParentStorage, String sName, String sMediaType, byte[] pBytes, String sPass) {
    // Important: a common password for any of parent storage should not be set or
    //            should be different from sPass

    try {
      Object oSubStream = xParentStorage.openStreamElement(sName, ElementModes.READ);
      Error("Encrypted stream '" + sName + "' was opened without password!");
      return false;
    } catch (WrongPasswordException wpe) {
    } catch (Exception e) {
      Error(
          "Unexpected exception in case of opening of encrypted stream '"
              + sName
              + "' without password: "******"!");
      return false;
    }

    String sWrongPass = "******";
    sWrongPass += sPass;
    try {
      Object oSubStream =
          xParentStorage.openEncryptedStreamElement(sName, ElementModes.READ, sWrongPass);
      Error("Encrypted stream '" + sName + "' was opened with wrong password!");
      return false;
    } catch (WrongPasswordException wpe) {
    } catch (Exception e) {
      Error(
          "Unexpected exception in case of opening of encrypted stream '"
              + sName
              + "' with wrong password: "******"!");
      return false;
    }

    XStream xSubStream = null;
    try {
      Object oSubStream =
          xParentStorage.openEncryptedStreamElement(sName, ElementModes.READ, sPass);
      xSubStream = (XStream) UnoRuntime.queryInterface(XStream.class, oSubStream);
      if (xSubStream == null) {
        Error("Can't open encrypted substream '" + sName + "'!");
        return false;
      }
    } catch (Exception e) {
      Error("Can't open encrypted substream '" + sName + "', exception : " + e + "!");
      return false;
    }

    // encrypted streams will be compressed always, so after the storing this property is always
    // true,
    // although before the storing it can be set to false ( it is not always clear whether a stream
    // is encrypted
    // before the storing )
    boolean bResult = InternalCheckStream(xSubStream, sName, sMediaType, true, pBytes, false);

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

    return bResult;
  }