Get ByteArrayOutputStream from FileDropTarget

Hello everybody!

I have a problem trying to get an OutputStream of type ByteArray from a FileDropTarget event. I managed to get a FileOutputStream, but I don’t want to write a file into the server, just get a bytearray to work on it and at the end save it in the disk.

I’m using vaadin 8.3.3 on Eclipse Oxygen.2 Release (4.7.2).

Here is some part of the code which is the same on the documentation site of Drag & Drop feature:

new FileDropTarget<>(dropArea, event -> {
			Collection<Html5File> files = event.getFiles();
			files.forEach(file -> {
				if ((file.getFileSize() <= 1024 * 1024) && (file.getType()).equals("application/pdf")) {
					file.setStreamVariable(new StreamVariable() {
						private static final long serialVersionUID = 1L;

						// Output stream to write the file to
		                @Override
		                public OutputStream getOutputStream() {	
		                	try{
		                        return new FileOutputStream(basepath + file.getFileName());
		                    } catch (FileNotFoundException e) {
		                    	e.printStackTrace();
		                    }
		                    return null;
		                }

Try a https://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html

thanks Ronny but I’ve already done that, I changed the return type of the method getOutputStream to ByteArrayOutputStream and then I invoke this method to get the outputStream, I do a toByteArray() but the size of the created array is ZERO, so it means something is wrong.