View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.spi.connector.transport;
20  
21  import java.io.ByteArrayInputStream;
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.net.URI;
26  import java.nio.charset.StandardCharsets;
27  import java.nio.file.Files;
28  
29  /**
30   * A task to upload a resource to the remote repository.
31   *
32   * @see Transporter#put(PutTask)
33   */
34  public final class PutTask extends TransportTask {
35  
36      private File dataFile;
37  
38      private byte[] dataBytes = EMPTY;
39  
40      /**
41       * Creates a new task for the specified remote resource.
42       *
43       * @param location The relative location of the resource in the remote repository, must not be {@code null}.
44       */
45      public PutTask(URI location) {
46          setLocation(location);
47      }
48  
49      /**
50       * Opens an input stream for the data to be uploaded. The length of the stream can be queried via
51       * {@link #getDataLength()}. It's the responsibility of the caller to close the provided stream.
52       *
53       * @return The input stream for the data, never {@code null}. The stream is unbuffered.
54       * @throws IOException If the stream could not be opened.
55       */
56      public InputStream newInputStream() throws IOException {
57          if (dataFile != null) {
58              return Files.newInputStream(dataFile.toPath());
59          }
60          return new ByteArrayInputStream(dataBytes);
61      }
62  
63      /**
64       * Gets the total number of bytes to be uploaded.
65       *
66       * @return The total number of bytes to be uploaded.
67       */
68      public long getDataLength() {
69          if (dataFile != null) {
70              return dataFile.length();
71          }
72          return dataBytes.length;
73      }
74  
75      /**
76       * Gets the file (if any) with the data to be uploaded.
77       *
78       * @return The data file or {@code null} if the data resides in memory.
79       */
80      public File getDataFile() {
81          return dataFile;
82      }
83  
84      /**
85       * Sets the file with the data to be uploaded. To upload some data residing already in memory, use
86       * {@link #setDataString(String)} or {@link #setDataBytes(byte[])}.
87       *
88       * @param dataFile The data file, may be {@code null} if the resource data is provided directly from memory.
89       * @return This task for chaining, never {@code null}.
90       */
91      public PutTask setDataFile(File dataFile) {
92          this.dataFile = dataFile;
93          dataBytes = EMPTY;
94          return this;
95      }
96  
97      /**
98       * Sets the binary data to be uploaded.
99       *
100      * @param bytes The binary data, may be {@code null}.
101      * @return This task for chaining, never {@code null}.
102      */
103     public PutTask setDataBytes(byte[] bytes) {
104         this.dataBytes = (bytes != null) ? bytes : EMPTY;
105         dataFile = null;
106         return this;
107     }
108 
109     /**
110      * Sets the textual data to be uploaded. The text is encoded using UTF-8 before transmission.
111      *
112      * @param str The textual data, may be {@code null}.
113      * @return This task for chaining, never {@code null}.
114      */
115     public PutTask setDataString(String str) {
116         return setDataBytes((str != null) ? str.getBytes(StandardCharsets.UTF_8) : null);
117     }
118 
119     /**
120      * Sets the listener that is to be notified during the transfer.
121      *
122      * @param listener The listener to notify of progress, may be {@code null}.
123      * @return This task for chaining, never {@code null}.
124      */
125     public PutTask setListener(TransportListener listener) {
126         super.setListener(listener);
127         return this;
128     }
129 
130     @Override
131     public String toString() {
132         return ">> " + getLocation();
133     }
134 }