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