Example usage for java.io BufferedOutputStream flush

List of usage examples for java.io BufferedOutputStream flush

Introduction

In this page you can find the example usage for java.io BufferedOutputStream flush.

Prototype

@Override
public synchronized void flush() throws IOException 

Source Link

Document

Flushes this buffered output stream.

Usage

From source file:JarHelper.java

/**
 * Given an InputStream on a jar file, unjars the contents into the given
 * directory./*from w ww  .  j  a  v  a2 s .c  om*/
 */
public void unjar(InputStream in, File destDir) throws IOException {
    BufferedOutputStream dest = null;
    JarInputStream jis = new JarInputStream(in);
    JarEntry entry;
    while ((entry = jis.getNextJarEntry()) != null) {
        if (entry.isDirectory()) {
            File dir = new File(destDir, entry.getName());
            dir.mkdir();
            if (entry.getTime() != -1)
                dir.setLastModified(entry.getTime());
            continue;
        }
        int count;
        byte data[] = new byte[BUFFER_SIZE];
        File destFile = new File(destDir, entry.getName());
        if (mVerbose)
            System.out.println("unjarring " + destFile + " from " + entry.getName());
        FileOutputStream fos = new FileOutputStream(destFile);
        dest = new BufferedOutputStream(fos, BUFFER_SIZE);
        while ((count = jis.read(data, 0, BUFFER_SIZE)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
        if (entry.getTime() != -1)
            destFile.setLastModified(entry.getTime());
    }
    jis.close();
}

From source file:fr.gouv.finances.cp.xemelios.importers.batch.BatchRealImporter.java

private ImportContent files(final String extension, final String titreEtat) {
    ImportContent ic = new ImportContent();
    Vector<File> ret = new Vector<File>();
    ret.addAll(files);/*from   ww w . j av  a2 s  .com*/
    // on regarde si l'un des fichiers a importer est un zip
    for (int i = 0; i < ret.size(); i++) {
        if (ret.get(i).getName().toLowerCase().endsWith(".zip")) {
            if (ret.get(i).exists()) {
                ZipFile zf = null;
                try {
                    zf = new ZipFile(ret.get(i));
                    for (Enumeration<? extends ZipEntry> enumer = zf.entries(); enumer.hasMoreElements();) {
                        ZipEntry ze = enumer.nextElement();
                        if (!ze.isDirectory()) {
                            String fileName = ze.getName();
                            String entryName = fileName.toLowerCase();
                            fileName = fileName.replace(File.pathSeparatorChar, '_')
                                    .replace(File.separatorChar, '_').replace(':', '|').replace('\'', '_')
                                    .replace('/', '_');
                            logger.debug(entryName);
                            if (PJRef.isPJ(ze)) {
                                PJRef pj = new PJRef(ze);
                                File tmpFile = pj.writeTmpFile(FileUtils.getTempDir(), zf);
                                ic.pjs.add(pj);
                                filesToDrop.add(tmpFile);
                            } else if ((entryName.endsWith(extension.toLowerCase())
                                    || entryName.endsWith(".xml")) && !fileName.startsWith("_")) {
                                // on decompresse le fichier dans le
                                // repertoire temporaire, comme ca il sera
                                // supprime en quittant
                                InputStream is = zf.getInputStream(ze);
                                BufferedInputStream bis = new BufferedInputStream(is);
                                File output = new File(FileUtils.getTempDir(), fileName);
                                BufferedOutputStream bos = new BufferedOutputStream(
                                        new FileOutputStream(output));
                                byte[] buffer = new byte[1024];
                                int read = bis.read(buffer);
                                while (read > 0) {
                                    bos.write(buffer, 0, read);
                                    read = bis.read(buffer);
                                }
                                bos.flush();
                                bos.close();
                                bis.close();
                                ic.filesToImport.add(output);
                                filesToDrop.add(output);
                            }
                        }
                    }
                    zf.close();
                } catch (ZipException zEx) {
                    System.out.println(
                            "Le fichier " + ret.get(i).getName() + " n'est pas une archive ZIP valide.");
                } catch (IOException ioEx) {
                    ioEx.printStackTrace();
                } finally {
                    if (zf != null) {
                        try {
                            zf.close();
                        } catch (Throwable t) {
                        }
                    }
                }
            }
        } else if (ret.get(i).getName().toLowerCase().endsWith(".gz")) {
            try {
                String fileName = ret.get(i).getName();
                fileName = fileName.substring(0, fileName.length() - 3);
                File output = new File(FileUtils.getTempDir(), fileName);
                GZIPInputStream gis = new GZIPInputStream(new FileInputStream(ret.get(i)));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output));
                byte[] buffer = new byte[1024];
                int read = gis.read(buffer);
                while (read > 0) {
                    bos.write(buffer, 0, read);
                    read = gis.read(buffer);
                }
                bos.flush();
                bos.close();
                gis.close();
                ic.filesToImport.add(output);
                filesToDrop.add(output);
            } catch (IOException ioEx) {
                // nothing to do
            }
        } else {
            ic.filesToImport.add(ret.get(i));
            // dans ce cas l, on ne le supprime pas
        }
    }
    return ic;
}

From source file:org.commoncrawl.service.listcrawler.ListUploadServlet.java

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    String customerId = req.getParameter("customerId");
    String listName = req.getParameter("listName");
    String incomingFileName = req.getParameter("fileName");

    LOG.info("###LISTUPLOADER: GOT PUT Customer Id:" + customerId + " ListName:" + listName + " FileName:"
            + incomingFileName);//from   w  ww .j  av a2  s .  co m
    if (customerId == null || !customers.contains(customerId) || listName == null || listName.length() == 0) {
        LOG.error(
                "###LISTUPLOADER:No Customer Id or Invalid Customer Id:" + customerId + " ListId:" + listName);
        resp.sendError(500, "Invalid Customer Id or Invalid List Name!" + customerId + ":" + listName);
        return;
    } else if (incomingFileName == null || incomingFileName.length() == 0) {
        LOG.error("###LISTUPLOADER:No IncomingFilename");
        resp.sendError(500, "Invalid Filename");
        return;
    } else {
        // get the server ... 
        ProxyServer server = ProxyServer.getSingleton();
        // get the crawl history data directory ...
        File dataDir = server.getCrawlHistoryDataDir();
        // create import file ... 
        File importFile = new File(dataDir, incomingFileName + "-" + System.currentTimeMillis());
        LOG.info("###LISTUPLOADER:Filename:" + incomingFileName + " Customer:" + customerId + " List:"
                + listName + " outputFile:" + importFile.getAbsolutePath());
        // open a handle to it 
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(importFile), 1 << 20);
        // allocate a buffer ... 
        byte incomingBuffer[] = new byte[1 << 19];
        int bytesRead = -1;
        int totalBytesRead = 0;
        // get input stream 
        InputStream input = req.getInputStream();
        try {
            try {
                while ((bytesRead = input.read(incomingBuffer)) != -1) {
                    LOG.info("Read:" + bytesRead + " bytes from:" + incomingFileName);
                    outputStream.write(incomingBuffer, 0, bytesRead);
                    totalBytesRead += bytesRead;
                }
            } finally {
                outputStream.flush();
                outputStream.close();
            }
            LOG.info("###LISTUPLOADER:List:" + listName + " Finished download filename:" + incomingFileName
                    + " TotalBytesRead:" + totalBytesRead + "-Inserting Record");
            // won't reach here unless write succeeded ... 
            // create a database record 
            CrawlListDatabaseRecord databaseRecord = new CrawlListDatabaseRecord();

            databaseRecord.setListName(listName);
            databaseRecord.setCustomerName(customerId);
            databaseRecord.setSourceFileName(incomingFileName);
            databaseRecord.setTempFileName(importFile.getName());

            long listId = server.queueListImportRequest(databaseRecord);

            LOG.info("###LISTUPLOADER:Queueing List:" + listName + " ListID:" + listId);

            if (listId == -1) {
                LOG.error("###LISTUPLOADER:Queueing For List:" + listName + " Failed!");
                resp.sendError(500, "Queue Request Failed!");
            } else {
                resp.setContentType("text/plain");
                resp.getWriter().print(Long.toString(listId));
                resp.getWriter().flush();
            }
        } catch (IOException e) {
            LOG.error("###LISTUPLOADER: IOException processing List:" + listName);
            LOG.error(CCStringUtils.stringifyException(e));
            importFile.delete();
        }
    }
}

From source file:com.globalsight.everest.webapp.pagehandler.administration.logs.ViewLogsHelper.java

/**
 * Download the package file of log files through HttpServletResponse
 *
 * @param zipFile/*from  w  w w  .j  a  va 2s . com*/
 *            Package file of log files
 */
public void doDownload(File zipFile) {
    BufferedOutputStream fout = null;
    BufferedInputStream fin = null;
    byte[] buffer = new byte[4096];
    int size = -1;

    if (zipFile != null) {
        try {
            fin = new BufferedInputStream(new FileInputStream(zipFile));
            fout = new BufferedOutputStream(response.getOutputStream());
            while ((size = fin.read(buffer)) != -1) {
                fout.write(buffer, 0, size);
            }
            fout.flush();
            fout.close();
            fin.close();

            zipFile.delete();
        } catch (IOException e) {
            logger.error("Cannot download full logs.", e);
        }
    }
}

From source file:com.gfan.sbbs.utils.images.ImageManager.java

/**
 * Bitmap./*from ww  w . ja  v a2  s .c o m*/
 * 
 * @param file
 *            URL/PATH
 * @param bitmap
 * @param quality
 */
private void writeFile(String file, Bitmap bitmap, int quality) {
    if (bitmap == null) {
        Log.w(TAG, "Can't write file. Bitmap is null.");
        return;
    }

    BufferedOutputStream bos = null;
    try {
        //         String hashedUrl = getMd5(file);
        String hashedUrl = file;
        bos = new BufferedOutputStream(mContext.openFileOutput(hashedUrl, Context.MODE_PRIVATE));
        bitmap.compress(Bitmap.CompressFormat.JPEG, quality, bos); // PNG
        Log.d(TAG, "Writing file: " + file);
    } catch (IOException ioe) {
        Log.e(TAG, ioe.getMessage());
    } finally {
        try {
            if (bos != null) {
                bitmap.recycle();
                bos.flush();
                bos.close();
            }
            // bitmap.recycle();
        } catch (IOException e) {
            Log.e(TAG, "Could not close file.");
        }
    }
}

From source file:gov.nih.nci.restgen.util.JarHelper.java

/**
 * Given an InputStream on a jar file, unjars the contents into the given
 * directory.//w ww. j a va2s .c  o m
 */
public void unjar(InputStream in, File destDir) throws IOException {
    BufferedOutputStream dest = null;
    JarInputStream jis = new JarInputStream(in);
    JarEntry entry;
    while ((entry = jis.getNextJarEntry()) != null) {
        if (entry.isDirectory()) {
            File dir = new File(destDir, entry.getName());
            dir.mkdir();
            if (entry.getTime() != -1)
                dir.setLastModified(entry.getTime());
            continue;
        }
        int count;
        byte data[] = new byte[BUFFER_SIZE];
        File destFile = new File(destDir, entry.getName());
        if (mVerbose) {
            //System.out.println("unjarring " + destFile + " from " + entry.getName());
        }
        FileOutputStream fos = new FileOutputStream(destFile);
        dest = new BufferedOutputStream(fos, BUFFER_SIZE);
        while ((count = jis.read(data, 0, BUFFER_SIZE)) != -1) {
            dest.write(data, 0, count);
        }
        dest.flush();
        dest.close();
        if (entry.getTime() != -1)
            destFile.setLastModified(entry.getTime());
    }
    jis.close();
}

From source file:es.alrocar.map.vector.provider.filesystem.impl.GeoJSONFileSystemProvider.java

@SuppressWarnings("unchecked")
public void save(int[] tile, int zoomLevel, String driverName, Cancellable cancellable, Object data) {
    StringBuffer file = new StringBuffer(baseDir).append(driverName).append(File.separator).append(zoomLevel)
            .append(File.separator).append(tile[0]).append(File.separator).append(tile[1]).append(".geojson");
    FileOutputStream fos = null;//from   w w w .j  a v a2 s.  c  om
    BufferedOutputStream bos = null;
    try {
        File f = new File(file.toString());

        if (f.exists()) {
            if (f.length() <= 0) {
                // log.log(Level.FINE, "Deleting invalid file");
                f.delete();
                f.createNewFile();
            }
        } else {

            File dir = new File(f.getParent());
            if (dir.mkdirs()) {

            } else {
                if (f.getParent() == null) {
                    // log.log(Level.FINE, "parent = null");
                }
                // log.log(Level.FINE, "directories failed " + aURLString);
            }

        }

        fos = new FileOutputStream(f);

        bos = new BufferedOutputStream(fos, Constants.IO_BUFFER_SIZE);

        // log.log(Level.FINE, "prepared to write " + aURLString);
        bos.write(data.toString().getBytes());
        bos.flush();
        // log.log(Level.FINE, "Tile stored " + aURLString);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } finally {
        Constants.closeStream(fos);
        Constants.closeStream(bos);
    }
}

From source file:com.mirth.connect.connectors.tcp.TcpDispatcher.java

@Override
public Response send(ConnectorProperties connectorProperties, ConnectorMessage message) {
    TcpDispatcherProperties tcpDispatcherProperties = (TcpDispatcherProperties) connectorProperties;
    Status responseStatus = Status.QUEUED;
    String responseData = null;/*from  www . j  a  va 2  s. c o m*/
    String responseStatusMessage = null;
    String responseError = null;
    boolean validateResponse = false;

    long dispatcherId = getDispatcherId();

    String socketKey = dispatcherId + tcpDispatcherProperties.getRemoteAddress()
            + tcpDispatcherProperties.getRemotePort();
    if (tcpDispatcherProperties.isOverrideLocalBinding()) {
        socketKey += tcpDispatcherProperties.getLocalAddress() + tcpDispatcherProperties.getLocalPort();
    }

    Socket socket = null;
    Thread timeoutThread = null;

    try {
        // Do some validation first to avoid unnecessarily creating sockets
        if (StringUtils.isBlank(tcpDispatcherProperties.getRemoteAddress())) {
            throw new Exception("Remote address is blank.");
        } else if (NumberUtils.toInt(tcpDispatcherProperties.getRemotePort()) <= 0) {
            throw new Exception("Remote port is invalid.");
        }

        socket = connectedSockets.get(socketKey);
        timeoutThread = timeoutThreads.get(socketKey);

        // If keep connection open is true, then interrupt the thread so it won't close the socket
        if (tcpDispatcherProperties.isKeepConnectionOpen() && timeoutThread != null) {
            disposeThreadQuietly(socketKey);
        }

        // Initialize a new socket if our current one is invalid, the remote side has closed, or keep connection open is false
        if (!tcpDispatcherProperties.isKeepConnectionOpen() || socket == null || socket.isClosed()
                || (tcpDispatcherProperties.isCheckRemoteHost() && socket instanceof StateAwareSocketInterface
                        && ((StateAwareSocketInterface) socket).remoteSideHasClosed())) {
            closeSocketQuietly(socketKey);

            logger.debug("Creating new socket (" + connectorProperties.getName() + " \"" + getDestinationName()
                    + "\" on channel " + getChannelId() + ").");
            String info = "Trying to connect on " + tcpDispatcherProperties.getRemoteAddress() + ":"
                    + tcpDispatcherProperties.getRemotePort() + "...";
            eventController.dispatchEvent(new ConnectionStatusEvent(getChannelId(), getMetaDataId(),
                    getDestinationName(), ConnectionStatusEventType.CONNECTING, info));

            if (tcpDispatcherProperties.isOverrideLocalBinding()) {
                socket = SocketUtil.createSocket(configuration, tcpDispatcherProperties.getLocalAddress(),
                        NumberUtils.toInt(tcpDispatcherProperties.getLocalPort()));
            } else {
                socket = SocketUtil.createSocket(configuration);
            }

            ThreadUtils.checkInterruptedStatus();
            connectedSockets.put(socketKey, socket);

            SocketUtil.connectSocket(socket, tcpDispatcherProperties.getRemoteAddress(),
                    NumberUtils.toInt(tcpDispatcherProperties.getRemotePort()), responseTimeout);

            socket.setReuseAddress(true);
            socket.setReceiveBufferSize(bufferSize);
            socket.setSendBufferSize(bufferSize);
            socket.setSoTimeout(responseTimeout);
            socket.setKeepAlive(tcpDispatcherProperties.isKeepConnectionOpen());

            eventController.dispatchEvent(new ConnectorCountEvent(getChannelId(), getMetaDataId(),
                    getDestinationName(), ConnectionStatusEventType.CONNECTED,
                    SocketUtil.getLocalAddress(socket) + " -> " + SocketUtil.getInetAddress(socket), true));
        }

        ThreadUtils.checkInterruptedStatus();

        // Send the message
        eventController.dispatchEvent(new ConnectionStatusEvent(getChannelId(), getMetaDataId(),
                getDestinationName(), ConnectionStatusEventType.SENDING,
                SocketUtil.getLocalAddress(socket) + " -> " + SocketUtil.getInetAddress(socket)));
        BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream(), bufferSize);
        BatchStreamReader batchStreamReader = new DefaultBatchStreamReader(socket.getInputStream());
        StreamHandler streamHandler = transmissionModeProvider.getStreamHandler(socket.getInputStream(), bos,
                batchStreamReader, tcpDispatcherProperties.getTransmissionModeProperties());
        streamHandler.write(getTemplateBytes(tcpDispatcherProperties, message));
        bos.flush();

        if (!tcpDispatcherProperties.isIgnoreResponse()) {
            ThreadUtils.checkInterruptedStatus();

            // Attempt to get the response from the remote endpoint
            try {
                String info = "Waiting for response from " + SocketUtil.getInetAddress(socket) + " (Timeout: "
                        + tcpDispatcherProperties.getResponseTimeout() + " ms)... ";
                eventController.dispatchEvent(new ConnectionStatusEvent(getChannelId(), getMetaDataId(),
                        getDestinationName(), ConnectionStatusEventType.WAITING_FOR_RESPONSE, info));
                byte[] responseBytes = streamHandler.read();
                if (responseBytes != null) {
                    responseData = new String(responseBytes,
                            CharsetUtils.getEncoding(tcpDispatcherProperties.getCharsetEncoding()));
                    responseStatusMessage = "Message successfully sent.";
                } else {
                    responseStatusMessage = "Message successfully sent, but no response received.";
                }

                streamHandler.commit(true);
                responseStatus = Status.SENT;

                // We only want to validate the response if we were able to retrieve it successfully
                validateResponse = tcpDispatcherProperties.getDestinationConnectorProperties()
                        .isValidateResponse();
            } catch (IOException e) {
                // An exception occurred while retrieving the response
                if (e instanceof SocketTimeoutException
                        || e.getCause() != null && e.getCause() instanceof SocketTimeoutException) {
                    responseStatusMessage = "Timeout waiting for response";

                    if (!tcpDispatcherProperties.isQueueOnResponseTimeout()) {
                        responseStatus = Status.ERROR;
                    }
                } else {
                    responseStatusMessage = "Error receiving response";
                }

                responseError = ErrorMessageBuilder.buildErrorMessage(connectorProperties.getName(),
                        responseStatusMessage + ": " + e.getMessage(), e);
                logger.warn(responseStatusMessage + " (" + connectorProperties.getName() + " \""
                        + getDestinationName() + "\" on channel " + getChannelId() + ").", e);
                eventController.dispatchEvent(new ErrorEvent(getChannelId(), getMetaDataId(),
                        message.getMessageId(), ErrorEventType.DESTINATION_CONNECTOR, getDestinationName(),
                        connectorProperties.getName(), responseStatusMessage + ".", e));
                eventController.dispatchEvent(new ConnectionStatusEvent(getChannelId(), getMetaDataId(),
                        getDestinationName(), ConnectionStatusEventType.FAILURE,
                        responseStatusMessage + " from " + SocketUtil.getInetAddress(socket)));

                closeSocketQuietly(socketKey);
            }
        } else {
            try {
                // MIRTH-2980: Since we're ignoring responses, flush out the socket's input stream so it doesn't continually grow
                socket.getInputStream().skip(socket.getInputStream().available());
            } catch (IOException e) {
                logger.warn("Error flushing socket input stream.", e);
            }

            // We're ignoring the response, so always return a successful response
            responseStatus = Status.SENT;
            responseStatusMessage = "Message successfully sent.";
        }

        if (tcpDispatcherProperties.isKeepConnectionOpen() && (getCurrentState() == DeployedState.STARTED
                || getCurrentState() == DeployedState.STARTING)) {
            if (sendTimeout > 0) {
                // Close the connection after the send timeout has been reached
                startThread(socketKey);
            }
        } else {
            // If keep connection open is false, then close the socket right now
            closeSocketQuietly(socketKey);
        }
    } catch (Throwable t) {
        disposeThreadQuietly(socketKey);
        closeSocketQuietly(socketKey);

        String monitorMessage = "Error sending message (" + SocketUtil.getLocalAddress(socket) + " -> "
                + SocketUtil.getInetAddress(socket) + "): " + t.getMessage();
        eventController.dispatchEvent(new ConnectionStatusEvent(getChannelId(), getMetaDataId(),
                getDestinationName(), ConnectionStatusEventType.FAILURE, monitorMessage));

        // If an exception occurred then close the socket, even if keep connection open is true
        responseStatusMessage = t.getClass().getSimpleName() + ": " + t.getMessage();
        responseError = ErrorMessageBuilder.buildErrorMessage(connectorProperties.getName(), t.getMessage(), t);

        String logMessage = "Error sending message via TCP (" + connectorProperties.getName() + " \""
                + getDestinationName() + "\" on channel " + getChannelId() + ").";

        if (t instanceof InterruptedException) {
            Thread.currentThread().interrupt();
        } else if (t instanceof ConnectException
                || t.getCause() != null && t.getCause() instanceof ConnectException) {
            if (isQueueEnabled()) {
                logger.warn(logMessage, t);
            } else {
                logger.error(logMessage, t);
            }
        } else {
            logger.debug(logMessage, t);
        }

        eventController.dispatchEvent(new ErrorEvent(getChannelId(), getMetaDataId(), message.getMessageId(),
                ErrorEventType.DESTINATION_CONNECTOR, getDestinationName(), connectorProperties.getName(),
                "Error sending message via TCP.", t));
    } finally {
        eventController.dispatchEvent(new ConnectorCountEvent(getChannelId(), getMetaDataId(),
                getDestinationName(), ConnectionStatusEventType.IDLE,
                SocketUtil.getLocalAddress(socket) + " -> " + SocketUtil.getInetAddress(socket),
                (Boolean) null));
    }

    return new Response(responseStatus, responseData, responseStatusMessage, responseError, validateResponse);
}

From source file:org.openwms.common.comm.tcp.OSIPTelegramSerializer.java

/**
 * Writes the source object to an output stream using Java Serialization. The source object must implement {@link Serializable}.
 *///from   ww  w . ja v  a 2  s  .  c om
@Override
public void serialize(Map<?, ?> map, OutputStream outputStream) throws IOException {
    BufferedOutputStream os = new BufferedOutputStream(outputStream);
    Map<String, String> headers = (Map<String, String>) map.get("headers");
    String header = String.valueOf(headers.get(CommHeader.SYNC_FIELD_NAME))
            + padLeft(
                    String.valueOf(CommConstants.TELEGRAM_LENGTH), CommHeader.LENGTH_MESSAGE_LENGTH_FIELD, "0")
            + String.valueOf(headers.get(CommHeader.SENDER_FIELD_NAME))
            + String.valueOf(headers.get(CommHeader.RECEIVER_FIELD_NAME)
                    + padLeft(String.valueOf(headers.get(CommHeader.SEQUENCE_FIELD_NAME)),
                            CommHeader.LENGTH_SEQUENCE_NO_FIELD, "0"));
    String s = header + ((Payload) map.get("payload")).asString();
    if (s.length() > CommConstants.TELEGRAM_LENGTH) {
        throw new MessageMismatchException("Defined telegram length exceeded, size is" + s.length());
    }
    os.write(padRight(s, CommConstants.TELEGRAM_LENGTH, CommConstants.TELEGRAM_FILLER_CHARACTER)
            .getBytes(Charset.defaultCharset()));
    os.write(CRLF);
    os.flush();
}

From source file:org.apache.drill.exec.vector.complex.writer.TestJsonReader.java

License:asdf

@Test
public void drill_4479() throws Exception {
    try {/*from  ww  w  .  j  a v  a 2s.  c  o  m*/
        String dfs_temp = getDfsTestTmpSchemaLocation();
        File table_dir = new File(dfs_temp, "drill_4479");
        table_dir.mkdir();
        BufferedOutputStream os = new BufferedOutputStream(
                new FileOutputStream(new File(table_dir, "mostlynulls.json")));
        // Create an entire batch of null values for 3 columns
        for (int i = 0; i < JSONRecordReader.DEFAULT_ROWS_PER_BATCH; i++) {
            os.write("{\"a\": null, \"b\": null, \"c\": null}".getBytes());
        }
        // Add a row with {bigint,  float, string} values
        os.write("{\"a\": 123456789123, \"b\": 99.999, \"c\": \"Hello World\"}".getBytes());
        os.flush();
        os.close();

        String query1 = "select c, count(*) as cnt from dfs_test.tmp.drill_4479 t group by c";
        String query2 = "select a, b, c, count(*) as cnt from dfs_test.tmp.drill_4479 t group by a, b, c";
        String query3 = "select max(a) as x, max(b) as y, max(c) as z from dfs_test.tmp.drill_4479 t";

        testBuilder().sqlQuery(query1).ordered()
                .optionSettingQueriesForTestQuery("alter session set `store.json.all_text_mode` = true")
                .baselineColumns("c", "cnt").baselineValues(null, 4096L).baselineValues("Hello World", 1L).go();

        testBuilder().sqlQuery(query2).ordered()
                .optionSettingQueriesForTestQuery("alter session set `store.json.all_text_mode` = true")
                .baselineColumns("a", "b", "c", "cnt").baselineValues(null, null, null, 4096L)
                .baselineValues("123456789123", "99.999", "Hello World", 1L).go();

        testBuilder().sqlQuery(query3).ordered()
                .optionSettingQueriesForTestQuery("alter session set `store.json.all_text_mode` = true")
                .baselineColumns("x", "y", "z").baselineValues("123456789123", "99.999", "Hello World").go();

    } finally {
        testNoResult("alter session set `store.json.all_text_mode` = false");
    }
}