Vaadin upload with PipedInputStream & PipedOutputStream example !

Hi,

I just started learning Vaadin 8 and my first example is Upload button. I was stuck with an issue where I could not solve the problem for many hours and hours.

Here it is,

I am returning PipedOutputStream in the receiveUpload method,

Here is the code for receiveUpload method,

  public OutputStream receiveUpload(String filename, String mimeType) {
  this.fileName = filename;
  this.mimeType = mimeType;
  try {

     pipedOutputStream = new PipedOutputStream();
     pipedInputStream = new PipedInputStream(pipedOutputStream);

    if (filename == null || filename.trim().length() == 0) {
        upload.interruptUpload();
     } else {
     }
  } catch (Exception e) {
     e.printStackTrace();
  }
  return pipedOutputStream;

}

In the uploadSucceeded method, i need to take the pipedinputstream and send it the another method to load the stream into database

  public void uploadSucceeded(SucceededEvent event) {
     try { 
        fileUploadOperation.upload(pipedInputStream); --> I need to push all the stream data in one go into a method to generate a file at the bussiess layer
     } catch (Exception e) {
        e.printStackTrace();
     }
  }

When I was running the application, it hangs out for long time and I could not figure out where it is. Later I could notice that both piped input and piped output streams should be created in separate threads or atleast one of them in a separate thread but dont know how to handle it.

Any help

public class WebCommunityView implements Receiver, FailedListener, SucceededListener, StartedListener, FinishedListener {

   private PipedOutputStream pipedOutputStream = null;
   private PipedInputStream pipedInputStream = null;
   private Upload upload = null;
   private String fileName = null, mimeType = null;
   private Grid<FileListProperties> fileListGrid = null;

   public final static WebCommunityView newInstance() {
	  vw.initBody();
	  return vw;
   }

   protected void initBody() {

	  VerticalLayout verticalLayout = new VerticalLayout();

	  fileListGrid = new Grid<FileListProperties>();
	  fileListGrid.addColumn(FileListProperties::getCreatedDate).setCaption("Date");
	  fileListGrid.addColumn(FileListProperties::getFileName).setCaption("File Name");
	  fileListGrid.addColumn(FileListProperties::getUserName).setCaption("User Name");
	  fileListGrid.addComponentColumn(this::buildDownloadButton).setCaption("Download");

	  fileListGrid.setItems(loadGridWithFileInfo());

	  upload = new Upload("", this);
	  upload.setImmediateMode(false);
	  upload.addFailedListener((Upload.FailedListener) this);
	  upload.addSucceededListener((Upload.SucceededListener) this);
	  upload.addStartedListener((Upload.StartedListener) this);
	  upload.addFinishedListener((Upload.FinishedListener) this);

	  Label fileUploadLabel = new Label("Label"));

	  verticalLayout.addComponent(currentListLabel);
	  verticalLayout.addComponent(fileListGrid);
	  verticalLayout.addComponent(fileUploadLabel);
	  verticalLayout.addComponent(upload);
	  mainbody.addComponent(verticalLayout);
   }

   @Override
   public void uploadSucceeded(SucceededEvent event) {

		 try {
			//Model Layer
			fileUploadOperation.upload(pipedInputStream);
			fileUploadOperation.commit();

		 } catch (Exception e) {
			e.printStackTrace();
		 }

   }

   @Override
   public void uploadFailed(FailedEvent event) {
	  if (event.getFilename() == null) {
		 Notification.show("Upload failed", Notification.Type.HUMANIZED_MESSAGE);
	  }

		 try {
						//Model Layer
			fileUploadOperation.abort();
		 } catch (Exception e) {
			e.printStackTrace();
		 }
   }


   public OutputStream receiveUpload(String filename, String mimeType) {
	  this.fileName = filename;
	  this.mimeType = mimeType;
	  try {

		 pipedOutputStream = new PipedOutputStream();
		 
		 new Thread() {
			public void run() {
			   try {
				  System.out.println("pipedInputStream Thread started");
				  pipedInputStream = new PipedInputStream(pipedOutputStream);
				  
			   } catch (Exception e) {
				  e.printStackTrace();
			   }
			};
		 }.start();
		 
		 if (filename == null || filename.trim().length() == 0) {
			screen.displayMessage("Please select a file to upload !", WebContentScreen.MESSAGE_TYPE_WARNING);
			upload.interruptUpload();
		 } else {
			Properties properties = new Properties();
			properties.setProperty("NAME", fileName);
			properties.setProperty("MIME_TYPE", mimeType);
			//Model Layer
			fileUploadOperation.initialize(properties);
	
		 }
	  } catch (Exception e) {
		 e.printStackTrace();
	  }
	  System.out.println("pipedOutputStream:"+pipedOutputStream);
	  return pipedOutputStream;
   }

   private List<FileListProperties> loadGridWithFileInfo() {
	  List<FileListProperties> list = null;
	  DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy");

	  try {
		 list = new ArrayList<FileListProperties>(1);
		 Collection<FileInfo> fileInfoList = fileCommandQuery.lstFilesForDownload();
		 for (Iterator iterator = fileInfoList.iterator(); iterator.hasNext();) {

			FileInfo fileInfo = (FileInfo) iterator.next();
			Properties properties = fileInfo.getProperties();

			Collection<String> mandatoryParameters = fileInfo.getMandatoryProperties();

			FileListProperties fileListProperties = new FileListProperties();

			for (Iterator iterator2 = mandatoryParameters.iterator(); iterator2.hasNext();) {
			   String key = (String) iterator2.next();
			   String value = properties.getProperty(key);
			   if (key != null && key.equalsIgnoreCase("NAME")) {
				  fileListProperties.setFileName(value);
			   } else if (key != null && key.equalsIgnoreCase("USER_NAME")) {
				  fileListProperties.setUserName(value);
			   } else if (key != null && key.equalsIgnoreCase("CREATED_DATE")) {
				  Calendar calendar = Calendar.getInstance();
				  calendar.setTimeInMillis(1550566760000L);
				  fileListProperties.setCreatedDate(dateFormat.format(calendar.getTime()));
			   }
			}
			if (fileListProperties != null) {
			   list.add(fileListProperties);
			}
		 }

	  } catch (Exception e) {
		 e.printStackTrace();
	  } finally {
		 dateFormat = null;
	  }
	  return list;
   }

   private Button buildDownloadButton(FileListProperties fileListProperties) {
	  Button button = new Button("...");
	  button.addClickListener(e -> downloadFile(fileListProperties));
	  return button;
   }

   private void downloadFile(FileListProperties fileListProperties) {

   }
}