public Path createCSV(User user, int dashId, int inDeviceId, PinType pinType, short pin, int... deviceIds) throws Exception { if (!DataStream.isValid(pin, pinType)) { throw new IllegalStateException("Wrong pin format."); } Path path = generateExportCSVPath(user.email, dashId, inDeviceId, pinType, pin); try (OutputStream output = Files.newOutputStream(path); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter( new GZIPOutputStream(output), CharsetUtil.US_ASCII))) { int emptyDataCounter = 0; for (int deviceId : deviceIds) { ByteBuffer onePinData = reportingDao.getByteBufferFromDisk(user, dashId, deviceId, pinType, pin, FETCH_COUNT, GraphGranularityType.MINUTE, 0); if (onePinData != null) { writeBufToCsv(writer, onePinData, deviceId); } else { emptyDataCounter++; } } if (emptyDataCounter == deviceIds.length) { throw new NoDataException(); } } return path; }