Exemples de code AndroidAndroid code examples

Cette rubrique présente des éléments de code importants pour la version Android du Kit RMS SDK.This topic will introduce you to important code elements for the Android version of the RMS SDK.

Remarque : Dans l’exemple de code et les descriptions qui suivent, nous utilisons le terme MSIPC (Microsoft Information Protection and Control) pour faire référence au processus client.Note In the example code and descriptions that follow, we use the term MSIPC (Microsoft Information Protection and Control) to reference the client process.

Utilisation de Microsoft Rights Management SDK 4.2 : principaux scénariosUsing the Microsoft Rights Management SDK 4.2 - key scenarios

Voici des exemples de code tirés d’un exemple d’application plus large représentant des scénarios de développement importants pour votre compréhension de ce SDK.Following are code examples from a larger sample application representing development scenarios important to your orientation to this SDK. Ces exemples illustrent l’utilisation du format de fichier protégé Microsoft (appelé « fichier protégé »), l’utilisation de formats de fichiers protégés personnalisés et l’utilisation de contrôles d’interface utilisateur personnalisés.These demonstrate; use of Microsoft Protected File format referred to as protected file, use of custom protected file formats, and use of custom UI controls.

L’exemple d’application, MSIPCSampleApp, est disponible pour une utilisation avec ce SDK pour le système d’exploitation Android.The sample application, MSIPCSampleApp, is available for use with this SDK for the Android operating system. Consultez rms-sdk-ui-for-android sur GitHub pour accéder à cette application.See rms-sdk-ui-for-android on GitHub for access to this sample application.

Scénario : Consommer un fichier protégé RMSScenario: Consume an RMS protected file

  • Étape 1 : Créer un ProtectedFileInputStreamStep 1: Create a ProtectedFileInputStream

    Source : MsipcAuthenticationCallback.javaSource: MsipcAuthenticationCallback.java

    Description : Instancier un objet ProtectedFileInputStream par le biais de sa méthode de création qui implémente l’authentification de service à l’aide de AuthenticationRequestCallback pour obtenir un jeton en passant une instance de AuthenticationRequestCallback comme paramètre mRmsAuthCallback à l’API MSIPC.Description: Instantiate a ProtectedFileInputStream object, through its create method which implements service authentication using the AuthenticationRequestCallback to get a token by passing an instance of AuthenticationRequestCallback, as the parameter mRmsAuthCallback, to the MSIPC API. Reportez-vous à l’appel à ProtectedFileInputStream.create vers la fin de la section de l’exemple de code suivant.See the call to ProtectedFileInputStream.create near the end of the following example code section.

    public void startContentConsumptionFromPtxtFileFormat(InputStream inputStream)
    {
        CreationCallback<ProtectedFileInputStream> protectedFileInputStreamCreationCallback =
          new CreationCallback<ProtectedFileInputStream>()
        {
            @Override
            public Context getContext()
            {
               …
           }
    
            @Override
            public void onCancel()
            {
               …
            }
    
            @Override
            public void onFailure(ProtectionException e)
            {
               …
            }
    
            @Override
            public void onSuccess(ProtectedFileInputStream protectedFileInputStream)
            {
               …
               …
                byte[] dataChunk = new byte[16384];
                try
                {
                    while ((nRead = protectedFileInputStream.read(dataChunk, 0,
                            dataChunk.length)) != -1)
                    {
                        …
                    }
                     …
                    protectedFileInputStream.close();
                }
                catch (IOException e)
                {
                  …
                }  
          }
        };
        try
        {
           …
            ProtectedFileInputStream.create(inputStream, null, mRmsAuthCallback,
                                            PolicyAcquisitionFlags.NONE,
                                            protectedFileInputStreamCreationCallback);
        }
        catch (com.microsoft.rightsmanagement.exceptions.InvalidParameterException e)
        {
            …
        }
    }
    
  • Étape 2 : Configurer l’authentification à l’aide de la bibliothèque d’authentification Active Directory (ADAL).Step 2: Setup authentication using the Active Directory Authentication Library (ADAL).

    Source : MsipcAuthenticationCallback.java.Source: MsipcAuthenticationCallback.java.

    Description : Dans cette étape, nous allons utiliser la bibliothèque ADAL pour implémenter un AuthenticationRequestCallback avec des exemples de paramètres d’authentification.Description: In this step you will see ADAL used to implement an AuthenticationRequestCallback with example authentication parameters. Pour plus d’informations sur l’utilisation de la bibliothèque ADAL, consultez la Bibliothèque d’authentification Azure AD (ADAL).For more information on using ADAL, see the Azure AD Authentication Library (ADAL).

    class MsipcAuthenticationCallback implements AuthenticationRequestCallback
    {

    …

    @Override
    public void getToken(Map<String, String> authenticationParametersMap,
                         final AuthenticationCompletionCallback authenticationCompletionCallbackToMsipc)
    {
        String authority = authenticationParametersMap.get("oauth2.authority");
        String resource = authenticationParametersMap.get("oauth2.resource");
        String userId = authenticationParametersMap.get("userId");
        final String userHint = (userId == null)? "" : userId;
        AuthenticationContext authenticationContext = App.getInstance().getAuthenticationContext();
        if (authenticationContext == null || !authenticationContext.getAuthority().equalsIgnoreCase(authority))
        {
            try
            {
                authenticationContext = new AuthenticationContext(App.getInstance().getApplicationContext(), authority, …);
                App.getInstance().setAuthenticationContext(authenticationContext);
            }
            catch (NoSuchAlgorithmException e)
            {
                …
                authenticationCompletionCallbackToMsipc.onFailure();
            }
            catch (NoSuchPaddingException e)
            {
                …
                authenticationCompletionCallbackToMsipc.onFailure();
            }
       }
        App.getInstance().getAuthenticationContext().acquireToken(mParentActivity, resource, mClientId, mRedirectURI, userId, mPromptBehavior,
                       "&USERNAME=" + userHint, new AuthenticationCallback<AuthenticationResult>()
                        {
                            @Override
                            public void onError(Exception exc)
                            {
                                …
                                if (exc instanceof AuthenticationCancelError)
                                {
                                     …
                                    authenticationCompletionCallbackToMsipc.onCancel();
                                }
                                else
                                {
                                     …
                                    authenticationCompletionCallbackToMsipc.onFailure();
                                }
                            }

                            @Override
                            public void onSuccess(AuthenticationResult result)
                            {
                                …
                                if (result == null || result.getAccessToken() == null
                                        || result.getAccessToken().isEmpty())
                                {
                                     …
                                }
                                else
                                {
                                    // request is successful
                                    …
                                    authenticationCompletionCallbackToMsipc.onSuccess(result.getAccessToken());
                                }
                            }
                        }

                        );
                  }
  • Étape 3 : Vérifier l’existence de l’autorisation Edit pour cet utilisateur avec ce contenu via la méthode UserPolicy.accessCheck.Step 3: Check if the Edit right exists for this user with this content via the UserPolicy.accessCheck method.

    Source : TextEditorFragment.javaSource: TextEditorFragment.java

     //check if user has edit rights and apply enforcements
            if (!mUserPolicy.accessCheck(EditableDocumentRights.Edit))
            {
                mTextEditor.setFocusableInTouchMode(false);
                mTextEditor.setFocusable(false);
                mTextEditor.setEnabled(false);
                …
            }

Scénario : Créer un fichier protégé à l’aide d’un modèleScenario: Create a new protected file using a template

Ce scénario commence par obtenir une liste de modèles, sélectionne le premier pour créer une stratégie, puis nous crée et écrit dans le nouveau fichier protégé.This scenario begins with getting a list of templates, selecting the first one to create a policy, then creates and writes to the new protected file.

  • Étape 1 : Afficher la liste de modèles via un objet TemplateDescriptor.Step 1: Get list of templates via a TemplateDescriptor object.

    Source : MsipcTaskFragment.javaSource: MsipcTaskFragment.java

<span data-ttu-id="c2059-126">CreationCallback<List<TemplateDescriptor>> getTemplatesCreationCallback = new CreationCallback<List<TemplateDescriptor>>() { @Override public Context getContext() { …</span><span class="sxs-lookup"><span data-stu-id="c2059-126">CreationCallback<List<TemplateDescriptor>> getTemplatesCreationCallback = new CreationCallback<List<TemplateDescriptor>>() { @Override public Context getContext() { …</span></span>
      <span data-ttu-id="c2059-127">}</span><span class="sxs-lookup"><span data-stu-id="c2059-127">}</span></span>

      @Override
      public void onCancel()
      {
          …
      }

      @Override
      public void onFailure(ProtectionException e)
      {
          …
      }

      @Override
      public void onSuccess(List<TemplateDescriptor> templateDescriptors)
      {
         …
      }
  <span data-ttu-id="c2059-128">}; try { …</span><span class="sxs-lookup"><span data-stu-id="c2059-128">}; try { …</span></span>
      <span data-ttu-id="c2059-129">mIAsyncControl = TemplateDescriptor.getTemplates(emailId, mRmsAuthCallback, getTemplatesCreationCallback); } catch (com.microsoft.rightsmanagement.exceptions.InvalidParameterException e) { …</span><span class="sxs-lookup"><span data-stu-id="c2059-129">mIAsyncControl = TemplateDescriptor.getTemplates(emailId, mRmsAuthCallback, getTemplatesCreationCallback); } catch (com.microsoft.rightsmanagement.exceptions.InvalidParameterException e) { …</span></span>
  <span data-ttu-id="c2059-130">}</span><span class="sxs-lookup"><span data-stu-id="c2059-130">}</span></span>
  • Étape 2 : Créer un UserPolicy en utilisant le premier modèle de la liste.Step 2: Create a UserPolicy using the first template in the list.

    Source : MsipcTaskFragment.javaSource: MsipcTaskFragment.java

  <span data-ttu-id="c2059-133">CreationCallback<UserPolicy> userPolicyCreationCallback = new CreationCallback<UserPolicy>() { @Override public Context getContext() { …</span><span class="sxs-lookup"><span data-stu-id="c2059-133">CreationCallback<UserPolicy> userPolicyCreationCallback = new CreationCallback<UserPolicy>() { @Override public Context getContext() { …</span></span>
      <span data-ttu-id="c2059-134">}</span><span class="sxs-lookup"><span data-stu-id="c2059-134">}</span></span>

      @Override
      public void onCancel()
      {
          …
      }

      @Override
      public void onFailure(ProtectionException e)
      {
          …
      }

      @Override
      public void onSuccess(final UserPolicy item)
      {
          …
      }
  <span data-ttu-id="c2059-135">}; try { …</span><span class="sxs-lookup"><span data-stu-id="c2059-135">}; try { …</span></span>
      <span data-ttu-id="c2059-136">mIAsyncControl = UserPolicy.create((TemplateDescriptor)selectedDescriptor, mEmailId, mRmsAuthCallback, UserPolicyCreationFlags.NONE, userPolicyCreationCallback); …</span><span class="sxs-lookup"><span data-stu-id="c2059-136">mIAsyncControl = UserPolicy.create((TemplateDescriptor)selectedDescriptor, mEmailId, mRmsAuthCallback, UserPolicyCreationFlags.NONE, userPolicyCreationCallback); …</span></span>
  <span data-ttu-id="c2059-137">} catch (InvalidParameterException e) { …</span><span class="sxs-lookup"><span data-stu-id="c2059-137">} catch (InvalidParameterException e) { …</span></span>
  <span data-ttu-id="c2059-138">}</span><span class="sxs-lookup"><span data-stu-id="c2059-138">}</span></span>
<span data-ttu-id="c2059-141">private void createPTxt(final byte[] contentToProtect) { …</span><span class="sxs-lookup"><span data-stu-id="c2059-141">private void createPTxt(final byte[] contentToProtect) { …</span></span>
        <span data-ttu-id="c2059-142">CreationCallback<ProtectedFileOutputStream> protectedFileOutputStreamCreationCallback = new CreationCallback<ProtectedFileOutputStream>() { @Override public Context getContext() { …</span><span class="sxs-lookup"><span data-stu-id="c2059-142">CreationCallback<ProtectedFileOutputStream> protectedFileOutputStreamCreationCallback = new CreationCallback<ProtectedFileOutputStream>() { @Override public Context getContext() { …</span></span>
            <span data-ttu-id="c2059-143">}</span><span class="sxs-lookup"><span data-stu-id="c2059-143">}</span></span>

            @Override
            public void onCancel()
            {
             …
            }

            @Override
            public void onFailure(ProtectionException e)
            {
             …
            }

            @Override
            public void onSuccess(ProtectedFileOutputStream protectedFileOutputStream)
            {
                try
                {
                    // write to this stream
                    protectedFileOutputStream.write(contentToProtect);
                    protectedFileOutputStream.flush();
                    protectedFileOutputStream.close();
                    …
                }
                catch (IOException e)
                {
                    …
                }
            }
        };
        try
        {
            File file = new File(filePath);
            outputStream = new FileOutputStream(file);
            mIAsyncControl = ProtectedFileOutputStream.create(outputStream, mUserPolicy, originalFileExtension,
                    protectedFileOutputStreamCreationCallback);
        }
        catch (FileNotFoundException e)
        {
             …
        }
        catch (InvalidParameterException e)
        {
             …
        }
    <span data-ttu-id="c2059-144">}</span><span class="sxs-lookup"><span data-stu-id="c2059-144">}</span></span>

Scénario : Ouvrir un fichier protégé personnaliséScenario: Open a custom protected file

  • Étape 1 : Créer un UserPolicy à partir d’un serializedContentPolicy.Step 1: Create a UserPolicy from a serializedContentPolicy.

    Source : MsipcTaskFragment.javaSource: MsipcTaskFragment.java

<span data-ttu-id="c2059-148">CreationCallback<UserPolicy> userPolicyCreationCallbackFromSerializedContentPolicy = new CreationCallback<UserPolicy>() { @Override public void onSuccess(UserPolicy userPolicy) { …</span><span class="sxs-lookup"><span data-stu-id="c2059-148">CreationCallback<UserPolicy> userPolicyCreationCallbackFromSerializedContentPolicy = new CreationCallback<UserPolicy>() { @Override public void onSuccess(UserPolicy userPolicy) { …</span></span>
            <span data-ttu-id="c2059-149">}</span><span class="sxs-lookup"><span data-stu-id="c2059-149">}</span></span>

            @Override
            public void onFailure(ProtectionException e)
            {
              …
            }

            @Override
            public void onCancel()
            {
              …
            }

            @Override
            public Context getContext()
            {
              …
            }
        };


<span data-ttu-id="c2059-150">try {   ...</span><span class="sxs-lookup"><span data-stu-id="c2059-150">try {   ...</span></span>

  <span data-ttu-id="c2059-151">// Read the serializedContentPolicyLength from the inputStream.</span><span class="sxs-lookup"><span data-stu-id="c2059-151">// Read the serializedContentPolicyLength from the inputStream.</span></span>
  <span data-ttu-id="c2059-152">long serializedContentPolicyLength = readUnsignedInt(inputStream);</span><span class="sxs-lookup"><span data-stu-id="c2059-152">long serializedContentPolicyLength = readUnsignedInt(inputStream);</span></span>

  <span data-ttu-id="c2059-153">// Read the PL bytes from the input stream using the PL size.</span><span class="sxs-lookup"><span data-stu-id="c2059-153">// Read the PL bytes from the input stream using the PL size.</span></span>
  <span data-ttu-id="c2059-154">byte[] serializedContentPolicy = new byte[(int)serializedContentPolicyLength]; inputStream.read(serializedContentPolicy);</span><span class="sxs-lookup"><span data-stu-id="c2059-154">byte[] serializedContentPolicy = new byte[(int)serializedContentPolicyLength]; inputStream.read(serializedContentPolicy);</span></span>

  <span data-ttu-id="c2059-155">...</span><span class="sxs-lookup"><span data-stu-id="c2059-155">...</span></span>

  <span data-ttu-id="c2059-156">UserPolicy.acquire(serializedContentPolicy, null, mRmsAuthCallback, PolicyAcquisitionFlags.NONE,           userPolicyCreationCallbackFromSerializedContentPolicy); } catch (com.microsoft.rightsmanagement.exceptions.InvalidParameterException e) {   ... } catch (IOException e) {   ... }</span><span class="sxs-lookup"><span data-stu-id="c2059-156">UserPolicy.acquire(serializedContentPolicy, null, mRmsAuthCallback, PolicyAcquisitionFlags.NONE,           userPolicyCreationCallbackFromSerializedContentPolicy); } catch (com.microsoft.rightsmanagement.exceptions.InvalidParameterException e) {   ... } catch (IOException e) {   ... }</span></span>
  <span data-ttu-id="c2059-159">CreationCallback<CustomProtectedInputStream> customProtectedInputStreamCreationCallback = new CreationCallback<CustomProtectedInputStream>() { @Override public Context getContext() { …</span><span class="sxs-lookup"><span data-stu-id="c2059-159">CreationCallback<CustomProtectedInputStream> customProtectedInputStreamCreationCallback = new CreationCallback<CustomProtectedInputStream>() { @Override public Context getContext() { …</span></span>
     <span data-ttu-id="c2059-160">}</span><span class="sxs-lookup"><span data-stu-id="c2059-160">}</span></span>

     @Override
     public void onCancel()
     {
         …
     }

     @Override
     public void onFailure(ProtectionException e)
     {
         …
     }

     @Override
     public void onSuccess(CustomProtectedInputStream customProtectedInputStream)
     {
        …

         byte[] dataChunk = new byte[16384];
         try
         {
             while ((nRead = customProtectedInputStream.read(dataChunk, 0, dataChunk.length)) != -1)
             {
                  …
             }
              …
             customProtectedInputStream.close();
         }
         catch (IOException e)
         {
            …
         }
         …
     }
 <span data-ttu-id="c2059-161">};</span><span class="sxs-lookup"><span data-stu-id="c2059-161">};</span></span>

<span data-ttu-id="c2059-162">try {  ...</span><span class="sxs-lookup"><span data-stu-id="c2059-162">try {  ...</span></span>

  <span data-ttu-id="c2059-163">// Retrieve the encrypted content size.</span><span class="sxs-lookup"><span data-stu-id="c2059-163">// Retrieve the encrypted content size.</span></span>
  <span data-ttu-id="c2059-164">long encryptedContentLength = readUnsignedInt(inputStream);</span><span class="sxs-lookup"><span data-stu-id="c2059-164">long encryptedContentLength = readUnsignedInt(inputStream);</span></span>

  <span data-ttu-id="c2059-165">updateTaskStatus(new TaskStatus(TaskState.Starting, "Consuming content", true));</span><span class="sxs-lookup"><span data-stu-id="c2059-165">updateTaskStatus(new TaskStatus(TaskState.Starting, "Consuming content", true));</span></span>

  <span data-ttu-id="c2059-166">CustomProtectedInputStream.create(userPolicy, inputStream,                                 encryptedContentLength,                                 customProtectedInputStreamCreationCallback); } catch (com.microsoft.rightsmanagement.exceptions.InvalidParameterException e) {  ... } catch (IOException e) {  ... }</span><span class="sxs-lookup"><span data-stu-id="c2059-166">CustomProtectedInputStream.create(userPolicy, inputStream,                                 encryptedContentLength,                                 customProtectedInputStreamCreationCallback); } catch (com.microsoft.rightsmanagement.exceptions.InvalidParameterException e) {  ... } catch (IOException e) {  ... }</span></span>
<span data-ttu-id="c2059-169">@Override public void onSuccess(CustomProtectedInputStream customProtectedInputStream) {  mUserPolicy = customProtectedInputStream.getUserPolicy();  ByteArrayOutputStream buffer = new ByteArrayOutputStream();</span><span class="sxs-lookup"><span data-stu-id="c2059-169">@Override public void onSuccess(CustomProtectedInputStream customProtectedInputStream) {  mUserPolicy = customProtectedInputStream.getUserPolicy();  ByteArrayOutputStream buffer = new ByteArrayOutputStream();</span></span>

  <span data-ttu-id="c2059-170">int nRead;</span><span class="sxs-lookup"><span data-stu-id="c2059-170">int nRead;</span></span>                      
  <span data-ttu-id="c2059-171">byte[] dataChunk = new byte[16384];</span><span class="sxs-lookup"><span data-stu-id="c2059-171">byte[] dataChunk = new byte[16384];</span></span>

  <span data-ttu-id="c2059-172">try  {    while ((nRead = customProtectedInputStream.read(dataChunk, 0,                                                        dataChunk.length)) != -1)    {       buffer.write(dataChunk, 0, nRead);    }</span><span class="sxs-lookup"><span data-stu-id="c2059-172">try  {    while ((nRead = customProtectedInputStream.read(dataChunk, 0,                                                        dataChunk.length)) != -1)    {       buffer.write(dataChunk, 0, nRead);    }</span></span>

    <span data-ttu-id="c2059-173">buffer.flush();    mDecryptedContent = new String(buffer.toByteArray(), Charset.forName("UTF-8"));</span><span class="sxs-lookup"><span data-stu-id="c2059-173">buffer.flush();    mDecryptedContent = new String(buffer.toByteArray(), Charset.forName("UTF-8"));</span></span>

    <span data-ttu-id="c2059-174">buffer.close();    customProtectedInputStream.close();  }  catch (IOException e)  {    ...  } }</span><span class="sxs-lookup"><span data-stu-id="c2059-174">buffer.close();    customProtectedInputStream.close();  }  catch (IOException e)  {    ...  } }</span></span>

Scénario : Créer un fichier protégé personnalisé à l’aide d’une stratégie personnalisée (ad hoc)Scenario: Create a custom protected file using a custom (ad-hoc) policy

  • Étape 1 : Avec une adresse e-mail fournie par l’utilisateur, créer un descripteur de stratégie.Step 1: With an email address provided by the user, create a policy descriptor.

    Source : MsipcTaskFragment.javaSource: MsipcTaskFragment.java

    Description : Dans la pratique, les objets suivants sont créés en utilisant des entrées de l’utilisateur à partir de l’interface de l’appareil : UserRights et PolicyDescriptor.Description: In practice the following objects would be created by using user inputs from the device interface; UserRights and PolicyDescriptor.

  <span data-ttu-id="c2059-179">// create userRights list   UserRights userRights = new UserRights(Arrays.asList("consumer@domain.com"),     Arrays.asList( CommonRights.View, EditableDocumentRights.Print));   ArrayList<UserRights> usersRigthsList = new ArrayList<UserRights>();   usersRigthsList.add(userRights);</span><span class="sxs-lookup"><span data-stu-id="c2059-179">// create userRights list   UserRights userRights = new UserRights(Arrays.asList("consumer@domain.com"),     Arrays.asList( CommonRights.View, EditableDocumentRights.Print));   ArrayList<UserRights> usersRigthsList = new ArrayList<UserRights>();   usersRigthsList.add(userRights);</span></span>

  <span data-ttu-id="c2059-180">// Create PolicyDescriptor using userRights list   PolicyDescriptor policyDescriptor = PolicyDescriptor.createPolicyDescriptorFromUserRights(                                                          usersRigthsList);   policyDescriptor.setOfflineCacheLifetimeInDays(10);   policyDescriptor.setContentValidUntil(new Date());</span><span class="sxs-lookup"><span data-stu-id="c2059-180">// Create PolicyDescriptor using userRights list   PolicyDescriptor policyDescriptor = PolicyDescriptor.createPolicyDescriptorFromUserRights(                                                          usersRigthsList);   policyDescriptor.setOfflineCacheLifetimeInDays(10);   policyDescriptor.setContentValidUntil(new Date());</span></span>
  • Étape 2 : Créer un UserPolicy personnalisé à partir du descripteur de stratégie selectedDescriptor.Step 2: Create a custom UserPolicy from the policy descriptor, selectedDescriptor.

    Source : MsipcTaskFragment.javaSource: MsipcTaskFragment.java

   <span data-ttu-id="c2059-183">mIAsyncControl = UserPolicy.create((PolicyDescriptor)selectedDescriptor,                                          mEmailId, mRmsAuthCallback,                                          UserPolicyCreationFlags.NONE,                                          userPolicyCreationCallback);</span><span class="sxs-lookup"><span data-stu-id="c2059-183">mIAsyncControl = UserPolicy.create((PolicyDescriptor)selectedDescriptor,                                          mEmailId, mRmsAuthCallback,                                          UserPolicyCreationFlags.NONE,                                          userPolicyCreationCallback);</span></span>
<span data-ttu-id="c2059-186">File file = new File(filePath); final OutputStream outputStream = new FileOutputStream(file); CreationCallback<CustomProtectedOutputStream> customProtectedOutputStreamCreationCallback = new CreationCallback<CustomProtectedOutputStream>() { @Override public Context getContext() { …</span><span class="sxs-lookup"><span data-stu-id="c2059-186">File file = new File(filePath); final OutputStream outputStream = new FileOutputStream(file); CreationCallback<CustomProtectedOutputStream> customProtectedOutputStreamCreationCallback = new CreationCallback<CustomProtectedOutputStream>() { @Override public Context getContext() { …</span></span>
        <span data-ttu-id="c2059-187">}</span><span class="sxs-lookup"><span data-stu-id="c2059-187">}</span></span>

        @Override
        public void onCancel()
        {
          …
        }

        @Override
        public void onFailure(ProtectionException e)
        {
          …
        }

        @Override
        public void onSuccess(CustomProtectedOutputStream protectedOutputStream)
        {
            try
            {
                // write serializedContentPolicy
                byte[] serializedContentPolicy = mUserPolicy.getSerializedContentPolicy();
                writeLongAsUnsignedIntToStream(outputStream, serializedContentPolicy.length);
                outputStream.write(serializedContentPolicy);
                // write encrypted content
                if (contentToProtect != null)
                {
                    writeLongAsUnsignedIntToStream(outputStream,
                            CustomProtectedOutputStream.getEncryptedContentLength(contentToProtect.length,
                                    protectedOutputStream.getUserPolicy()));
                    protectedOutputStream.write(contentToProtect);
                    protectedOutputStream.flush();
                    protectedOutputStream.close();
                }
                else
                {
                    outputStream.flush();
                    outputStream.close();
                }
                …
            }
            catch (IOException e)
            {
              …
            }
        }
    };
    try
    {
        mIAsyncControl = CustomProtectedOutputStream.create(outputStream, mUserPolicy,
                customProtectedOutputStreamCreationCallback);
    }
    catch (InvalidParameterException e)
    {
      …
    }

CommentairesComments

Avant de transmettre vos commentaires, nous vous demandons de consulter notre règlement interne.Before commenting, we ask that you review our House rules.