Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if byte array is empty or not?

Tags:

java

resteasy

I am using following code to get uploaded file.

 @POST
    @Path("update")
    @Consumes(MediaType.WILDCARD)
    public boolean updateWorkBookMaster(MultipartFormDataInput input) {
        try {
            //get form data
            Map<String, List<InputPart>> uploadForm = input.getFormDataMap();

            //get uploaded file
            List<InputPart> inputParts = uploadForm.get("workBookFile");
            MultivaluedMap<String, String> header = inputParts.get(0).getHeaders();
            InputStream inputStream = inputParts.get(0).getBody(InputStream.class, null);
            byte[] bytes = IOUtils.toByteArray(inputStream);

now i want to check whether this byte[] is empty or not, while debugging when i have not uploaded any file it shows its length as 9, why its 9 not 0.

like image 316
Sagar Koshti Avatar asked May 20 '15 05:05

Sagar Koshti


People also ask

How do you know if a byte is empty?

There is no such thing as an empty byte (int, long, float, ...). Every byte (int, long, float, ...) has a value inside. Value can be zero, but that's is not empty. Zero is a legal value, just like any other.

How can check byte array is empty or not in C#?

To check if an given array is empty or not, we can use the built-in Array. Length property in C#. Alternatively, we can also use the Array. Length property to check if a array is null or empty in C#.

Can a byte array be null?

Only object references can be null; bytes can be zero, though, as you obviously know, and that's what you meant. In your code snippet, the loop is completely unnecessary; when you allocate the array, all the elements are guaranteed to be zero already.

How do you set an empty byte array?

In general Java terminology, an empty byte array is a byte array with length zero, and can be created with the Java expression new byte[0] .


1 Answers

You can implement null check for files this way:

import org.glassfish.jersey.media.multipart.ContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;

@POST
@Path("update")
@Consumes(MediaType.WILDCARD)
public boolean updateWorkBookMaster(FormDataMultiPart multiPartData) {
    try {
        final FormDataBodyPart workBookFilePart = multiPartData.getField("workBookFile");
        final ContentDisposition workBookFileDetails = workBookFilePart.getContentDisposition();
        final InputStream workBookFileDocument = workBookFilePart.getValueAs(InputStream.class);

        if (workBookFileDetails.getFileName() != null || 
            workBookFileDetails.getFileName().trim().length() > 0 ) {
            // file is present
        } else {
            // file is not uploadded
        }
    } ... // other code
}
like image 68
Pramod Karandikar Avatar answered Sep 29 '22 02:09

Pramod Karandikar