Class FileUpload
- All Implemented Interfaces:
Closeable,AutoCloseable,AttachedFile
This is used to upload data to discord for various purposes.
The InputStream will be closed on consumption by the request.
You can use close() to close the stream manually.
-
Method Summary
Modifier and TypeMethodDescriptionvoidaddPart(okhttp3.MultipartBody.Builder builder, int index) Used internally to build the multipart request.voidclaim()Marks this attachment as used and throws if it has already been used.voidclose()static FileUploadCreate a newFileUploadfor a byte array.static FileUploadCreate a newFileUploadfor a local file.static FileUploadCreate a newFileUploadfor a local file.static FileUploadfromData(InputStream data, String name) Create a newFileUploadfor an input stream.static FileUploadfromData(Path path, String name, OpenOption... options) Create a newFileUploadfor a local file.static FileUploadfromData(Path path, OpenOption... options) Create a newFileUploadfor a local file.getData()TheInputStreamrepresenting the data to upload as a file.getName()The filename for the file.booleanWhether this attached file has already been used.toAttachmentData(int index) Used internally to build attachment descriptions for requests.toString()
-
Method Details
-
fromData
Create a newFileUploadfor an input stream.
This is used to upload data to discord for various purposes.The
InputStreamwill be closed on consumption by the request. You can useclose()to close the stream manually.- Parameters:
data- TheInputStreamto uploadname- The representative name to use for the file- Returns:
FileUpload- Throws:
IllegalArgumentException- If null is provided or the name is empty- See Also:
-
fromData
Create a newFileUploadfor a byte array.
This is used to upload data to discord for various purposes.- Parameters:
data- Thebyte[]to uploadname- The representative name to use for the file- Returns:
FileUpload- Throws:
IllegalArgumentException- If null is provided or the name is empty
-
fromData
Create a newFileUploadfor a local file.
This is used to upload data to discord for various purposes.This opens a
FileInputStream, which will be closed on consumption by the request. You can useclose()to close the stream manually.- Parameters:
file- TheFileto uploadname- The representative name to use for the file- Returns:
FileUpload- Throws:
IllegalArgumentException- If null is provided or the name is emptyUncheckedIOException- If an IOException is thrown while opening the file- See Also:
-
fromData
Create a newFileUploadfor a local file.
This is used to upload data to discord for various purposes.This opens a
FileInputStream, which will be closed on consumption by the request. You can useclose()to close the stream manually.- Parameters:
file- TheFileto upload- Returns:
FileUpload- Throws:
IllegalArgumentException- If null is providedUncheckedIOException- If an IOException is thrown while opening the file- See Also:
-
fromData
@Nonnull public static FileUpload fromData(@Nonnull Path path, @Nonnull String name, @Nonnull OpenOption... options) Create a newFileUploadfor a local file.
This is used to upload data to discord for various purposes.This opens the path using
Files.newInputStream(Path, OpenOption...), which will be closed on consumption by the request. You can useclose()to close the stream manually.- Parameters:
path- ThePathof the file to uploadname- The representative name to use for the fileoptions- TheOpenOptionsspecifying how the file is opened- Returns:
FileUpload- Throws:
IllegalArgumentException- If null is provided or the name is emptyUncheckedIOException- If an IOException is thrown while opening the file
-
fromData
Create a newFileUploadfor a local file.
This is used to upload data to discord for various purposes. UsesPath.getFileName()to specify the name of the file, to customize the filename usefromData(Path, String, OpenOption...).This opens the path using
Files.newInputStream(Path, OpenOption...), which will be closed on consumption by the request. You can useclose()to close the stream manually.- Parameters:
path- ThePathof the file to uploadoptions- TheOpenOptionsspecifying how the file is opened- Returns:
FileUpload- Throws:
IllegalArgumentException- If null is providedUncheckedIOException- If an IOException is thrown while opening the file
-
getName
The filename for the file.- Returns:
- The filename
-
getData
TheInputStreamrepresenting the data to upload as a file.- Returns:
- The
InputStream
-
claim
public void claim()Description copied from interface:AttachedFileMarks this attachment as used and throws if it has already been used.
This does nothing onAttachmentUpdate.- Specified by:
claimin interfaceAttachedFile
-
isClaimed
public boolean isClaimed()Description copied from interface:AttachedFileWhether this attached file has already been used.
When this is true,AttachedFile.claim()will throw anIllegalStateException.Resources cannot be read multiple times, so repeated use of this instance is not allowed.
- Specified by:
isClaimedin interfaceAttachedFile- Returns:
- True if this attachment has already been used
-
addPart
Description copied from interface:AttachedFileUsed internally to build the multipart request.The index can be used as a unique identifier for the multipart name, which is required to be unique by Discord.
- Specified by:
addPartin interfaceAttachedFile- Parameters:
builder- TheMultipartBody.Builderused for the request bodyindex- The index of the attachment, ignored forAttachmentUpdate
-
toAttachmentData
Description copied from interface:AttachedFileUsed internally to build attachment descriptions for requests.
This contains the id/index of the attachment, and the name of the file.- Specified by:
toAttachmentDatain interfaceAttachedFile- Parameters:
index- The reference index (should be same asAttachedFile.addPart(MultipartBody.Builder, int))- Returns:
DataObjectfor the attachment
-
close
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Throws:
IOException
-
toString
-