| OLD | NEW | 
|    1 // Copyright 2016 The Chromium Authors. All rights reserved. |    1 // Copyright 2016 The Chromium Authors. All rights reserved. | 
|    2 // Use of this source code is governed by a BSD-style license that can be |    2 // Use of this source code is governed by a BSD-style license that can be | 
|    3 // found in the LICENSE file. |    3 // found in the LICENSE file. | 
|    4  |    4  | 
|    5 package org.chromium.net; |    5 package org.chromium.net; | 
|    6  |    6  | 
|    7 import android.os.ParcelFileDescriptor; |    7 import android.os.ParcelFileDescriptor; | 
|    8  |    8  | 
|    9 import java.io.File; |    9 import java.io.File; | 
|   10 import java.io.FileInputStream; |   10 import java.io.FileInputStream; | 
|   11 import java.io.IOException; |   11 import java.io.IOException; | 
|   12 import java.nio.ByteBuffer; |   12 import java.nio.ByteBuffer; | 
|   13 import java.nio.channels.FileChannel; |   13 import java.nio.channels.FileChannel; | 
|   14  |   14  | 
|   15 /** |   15 /** | 
|   16  * Provides implementations of {@link UploadDataProvider} for common use cases. |   16  * Provides implementations of {@link UploadDataProvider} for common use cases. | 
|   17  */ |   17  */ | 
|   18 public final class UploadDataProviders { |   18 public final class UploadDataProviders { | 
|   19     /** |   19     /** | 
|   20      * Uploads an entire file. |   20      * Uploads an entire file. | 
|   21      * |   21      * | 
|   22      * @param file The file to upload |   22      * @param file The file to upload | 
 |   23      * @return A new UploadDataProvider for the given file | 
|   23      */ |   24      */ | 
|   24     public static UploadDataProvider create(final File file) { |   25     public static UploadDataProvider create(final File file) { | 
|   25         return new FileUploadProvider(new FileChannelProvider() { |   26         return new FileUploadProvider(new FileChannelProvider() { | 
|   26             @Override |   27             @Override | 
|   27             public FileChannel getChannel() throws IOException { |   28             public FileChannel getChannel() throws IOException { | 
|   28                 return new FileInputStream(file).getChannel(); |   29                 return new FileInputStream(file).getChannel(); | 
|   29             } |   30             } | 
|   30         }); |   31         }); | 
|   31     } |   32     } | 
|   32  |   33  | 
|   33     /** |   34     /** | 
|   34      * Uploads an entire file, closing the descriptor when it is no longer neede
     d. |   35      * Uploads an entire file, closing the descriptor when it is no longer neede
     d. | 
|   35      * |   36      * | 
|   36      * @param fd The file descriptor to upload |   37      * @param fd The file descriptor to upload | 
|   37      * @throws IllegalArgumentException if {@code fd} is not a file. |   38      * @throws IllegalArgumentException if {@code fd} is not a file. | 
 |   39      * @return A new UploadDataProvider for the given file descriptor | 
|   38      */ |   40      */ | 
|   39     public static UploadDataProvider create(final ParcelFileDescriptor fd) { |   41     public static UploadDataProvider create(final ParcelFileDescriptor fd) { | 
|   40         return new FileUploadProvider(new FileChannelProvider() { |   42         return new FileUploadProvider(new FileChannelProvider() { | 
|   41             @Override |   43             @Override | 
|   42             public FileChannel getChannel() throws IOException { |   44             public FileChannel getChannel() throws IOException { | 
|   43                 if (fd.getStatSize() != -1) { |   45                 if (fd.getStatSize() != -1) { | 
|   44                     return new ParcelFileDescriptor.AutoCloseInputStream(fd).get
     Channel(); |   46                     return new ParcelFileDescriptor.AutoCloseInputStream(fd).get
     Channel(); | 
|   45                 } else { |   47                 } else { | 
|   46                     fd.close(); |   48                     fd.close(); | 
|   47                     throw new IllegalArgumentException("Not a file: " + fd); |   49                     throw new IllegalArgumentException("Not a file: " + fd); | 
|   48                 } |   50                 } | 
|   49             } |   51             } | 
|   50         }); |   52         }); | 
|   51     } |   53     } | 
|   52  |   54  | 
|   53     /** |   55     /** | 
|   54      * Uploads a ByteBuffer, from the current {@code buffer.position()} to {@cod
     e buffer.limit()} |   56      * Uploads a ByteBuffer, from the current {@code buffer.position()} to {@cod
     e buffer.limit()} | 
 |   57      * @param buffer The data to upload | 
 |   58      * @return A new UploadDataProvider for the given buffer | 
|   55      */ |   59      */ | 
|   56     public static UploadDataProvider create(ByteBuffer buffer) { |   60     public static UploadDataProvider create(ByteBuffer buffer) { | 
|   57         return new ByteBufferUploadProvider(buffer.slice()); |   61         return new ByteBufferUploadProvider(buffer.slice()); | 
|   58     } |   62     } | 
|   59  |   63  | 
|   60     /** Uploads {@code length} bytes from {@code data}, starting from {@code off
     set} */ |   64     /** | 
 |   65      * Uploads {@code length} bytes from {@code data}, starting from {@code offs
     et} | 
 |   66      * @param data Array containing data to upload | 
 |   67      * @param offset Offset within data to start with | 
 |   68      * @param length Number of bytes to upload | 
 |   69      * @return A new UploadDataProvider for the given data | 
 |   70      */ | 
|   61     public static UploadDataProvider create(byte[] data, int offset, int length)
      { |   71     public static UploadDataProvider create(byte[] data, int offset, int length)
      { | 
|   62         return new ByteBufferUploadProvider(ByteBuffer.wrap(data, offset, length
     )); |   72         return new ByteBufferUploadProvider(ByteBuffer.wrap(data, offset, length
     )); | 
|   63     } |   73     } | 
|   64  |   74  | 
|   65     /** Uploads the contents of {@code data} */ |   75     /** | 
 |   76      * Uploads the contents of {@code data} | 
 |   77      * @param data Array containing data to upload | 
 |   78      * @return A new UploadDataProvider for the given data | 
 |   79      */ | 
|   66     public static UploadDataProvider create(byte[] data) { |   80     public static UploadDataProvider create(byte[] data) { | 
|   67         return create(data, 0, data.length); |   81         return create(data, 0, data.length); | 
|   68     } |   82     } | 
|   69  |   83  | 
|   70     private interface FileChannelProvider { FileChannel getChannel() throws IOEx
     ception; } |   84     private interface FileChannelProvider { FileChannel getChannel() throws IOEx
     ception; } | 
|   71  |   85  | 
|   72     private static final class FileUploadProvider extends UploadDataProvider { |   86     private static final class FileUploadProvider extends UploadDataProvider { | 
|   73         private volatile FileChannel mChannel; |   87         private volatile FileChannel mChannel; | 
|   74         private final FileChannelProvider mProvider; |   88         private final FileChannelProvider mProvider; | 
|   75         /** Guards initalization of {@code mChannel} */ |   89         /** Guards initalization of {@code mChannel} */ | 
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  163         @Override |  177         @Override | 
|  164         public void rewind(UploadDataSink uploadDataSink) { |  178         public void rewind(UploadDataSink uploadDataSink) { | 
|  165             mUploadBuffer.position(0); |  179             mUploadBuffer.position(0); | 
|  166             uploadDataSink.onRewindSucceeded(); |  180             uploadDataSink.onRewindSucceeded(); | 
|  167         } |  181         } | 
|  168     } |  182     } | 
|  169  |  183  | 
|  170     // Prevent instantiation |  184     // Prevent instantiation | 
|  171     private UploadDataProviders() {} |  185     private UploadDataProviders() {} | 
|  172 } |  186 } | 
| OLD | NEW |