Search in sources :

Example 96 with BufferedWriter

use of java.io.BufferedWriter in project openhab1-addons by openhab.

the class AlarmDecoderBinding method connect.

private synchronized void connect() {
    try {
        // make sure we have disconnected
        disconnect();
        markAllItemsUnupdated();
        if (m_tcpHostName != null && m_tcpPort > 0) {
            m_socket = new Socket(m_tcpHostName, m_tcpPort);
            m_reader = new BufferedReader(new InputStreamReader(m_socket.getInputStream()));
            m_writer = new BufferedWriter(new OutputStreamWriter(m_socket.getOutputStream()));
            logger.info("connected to {}:{}", m_tcpHostName, m_tcpPort);
            startMsgReader();
        } else if (this.m_serialDeviceName != null) {
            /*
                 * by default, RXTX searches only devices /dev/ttyS* and
                 * /dev/ttyUSB*, and will so not find symlinks. The
                 * setProperty() call below helps
                 */
            updateSerialProperties(m_serialDeviceName);
            CommPortIdentifier ci = CommPortIdentifier.getPortIdentifier(m_serialDeviceName);
            CommPort cp = ci.open("openhabalarmdecoder", 10000);
            if (cp == null) {
                throw new IllegalStateException("cannot open serial port!");
            }
            if (cp instanceof SerialPort) {
                m_port = (SerialPort) cp;
            } else {
                throw new IllegalStateException("unknown port type");
            }
            m_port.setSerialPortParams(m_portSpeed, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
            m_port.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN | SerialPort.FLOWCONTROL_RTSCTS_OUT);
            m_port.disableReceiveFraming();
            m_port.disableReceiveThreshold();
            m_reader = new BufferedReader(new InputStreamReader(m_port.getInputStream()));
            m_writer = new BufferedWriter(new OutputStreamWriter(m_port.getOutputStream()));
            logger.info("connected to serial port: {}", m_serialDeviceName);
            startMsgReader();
        } else {
            logger.warn("alarmdecoder hostname or port not configured!");
        }
    } catch (PortInUseException e) {
        logger.error("cannot open serial port: {}, it is in use!", m_serialDeviceName);
    } catch (UnsupportedCommOperationException e) {
        logger.error("got unsupported operation {} on port {}", e.getMessage(), m_serialDeviceName);
    } catch (NoSuchPortException e) {
        logger.error("got no such port for {}", m_serialDeviceName);
    } catch (IllegalStateException e) {
        logger.error("got unknown port type for {}", m_serialDeviceName);
    } catch (UnknownHostException e) {
        logger.error("unknown host name :{}: ", m_tcpHostName, e);
    } catch (IOException e) {
        logger.error("cannot open connection to {}", m_connectString);
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) InputStreamReader(java.io.InputStreamReader) UnknownHostException(java.net.UnknownHostException) CommPortIdentifier(gnu.io.CommPortIdentifier) CommPort(gnu.io.CommPort) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) PortInUseException(gnu.io.PortInUseException) NoSuchPortException(gnu.io.NoSuchPortException) SerialPort(gnu.io.SerialPort) BufferedReader(java.io.BufferedReader) OutputStreamWriter(java.io.OutputStreamWriter) Socket(java.net.Socket)

Example 97 with BufferedWriter

use of java.io.BufferedWriter in project jodd by oblac.

the class PropsUtilTest method canUseBufferedWriterToWriteBasePropertiesToProps.

@Test
public void canUseBufferedWriterToWriteBasePropertiesToProps() throws IOException, URISyntaxException {
    final Properties properties = new Properties();
    properties.setProperty("myOneProperty", "and it's value");
    final StringWriter stringWriter = new StringWriter();
    final BufferedWriter writer = new BufferedWriter(stringWriter);
    safelyWritePropertiesToProps(writer, properties);
    final String expectedResourceFileName = getResourcePath("/singleProperty.props");
    final String actual = stringWriter.toString();
    //        assertEqualProps(actual, expectedResourceFileName);
    assertEqualsToPropsFile(actual, expectedResourceFileName);
}
Also used : StringWriter(java.io.StringWriter) Properties(java.util.Properties) BufferedWriter(java.io.BufferedWriter) Test(org.junit.Test)

Example 98 with BufferedWriter

use of java.io.BufferedWriter in project openhab1-addons by openhab.

the class AirConditioner method writeLine.

private void writeLine(String line) throws Exception {
    logger.debug("Sending request:'{}'", line);
    if (!isConnected()) {
        login();
    }
    BufferedWriter writer;
    try {
        writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        writer.write(line);
        writer.newLine();
        writer.flush();
    } catch (Exception e) {
        logger.debug("Could not write line. Disconnecting.", e);
        disconnect();
        throw (e);
    }
}
Also used : OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLException(javax.net.ssl.SSLException) SocketTimeoutException(java.net.SocketTimeoutException) BufferedWriter(java.io.BufferedWriter)

Example 99 with BufferedWriter

use of java.io.BufferedWriter in project openhab1-addons by openhab.

the class SerialConnector method connect.

public void connect() throws EHealthException {
    logger.debug("Going to open Serial connection on port '{}'", portName);
    // parse ports and if the default port is found, initialized the reader
    Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
    while (portList.hasMoreElements()) {
        CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
        if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
            if (id.getName().equals(portName)) {
                logger.debug("Serial port '{}' has been found.", portName);
                portId = id;
            }
        }
    }
    if (portId != null) {
        // initialize serial port
        try {
            serialPort = portId.open("openHAB", 2000);
            inputStream = serialPort.getInputStream();
            outputStream = serialPort.getOutputStream();
            bReader = new BufferedReader(new InputStreamReader(inputStream));
            bWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
            serialPort.addEventListener(this);
            serialPort.notifyOnDataAvailable(true);
            serialPort.setSerialPortParams(BAUD, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
        } catch (IOException e) {
            throw new EHealthException(e);
        } catch (PortInUseException e) {
            throw new EHealthException(e);
        } catch (UnsupportedCommOperationException e) {
            throw new EHealthException(e);
        } catch (TooManyListenersException e) {
            throw new EHealthException(e);
        }
    } else {
        StringBuilder sb = new StringBuilder();
        portList = CommPortIdentifier.getPortIdentifiers();
        while (portList.hasMoreElements()) {
            CommPortIdentifier id = (CommPortIdentifier) portList.nextElement();
            if (id.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                sb.append(id.getName() + "\n");
            }
        }
        throw new EHealthException("Serial port '" + portName + "' could not be found. Available ports are:\n" + sb.toString());
    }
}
Also used : UnsupportedCommOperationException(gnu.io.UnsupportedCommOperationException) TooManyListenersException(java.util.TooManyListenersException) PortInUseException(gnu.io.PortInUseException) InputStreamReader(java.io.InputStreamReader) CommPortIdentifier(gnu.io.CommPortIdentifier) BufferedReader(java.io.BufferedReader) EHealthException(org.openhab.binding.ehealth.internal.EHealthException) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Example 100 with BufferedWriter

use of java.io.BufferedWriter in project openhab1-addons by openhab.

the class LgTvChannelSet method savetofile.

/**
     * Save Channel List to File f
     * 
     * @param f
     */
public void savetofile(String f) {
    Writer writer = null;
    JAXBContext jc;
    try {
        jc = JAXBContext.newInstance(envelope.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f), "utf-8"));
        marshaller.marshal(envel, writer);
    } catch (PropertyException e) {
        logger.error("error in savetofile", e);
    } catch (JAXBException e) {
        logger.error("error in savetofile", e);
    } catch (IOException ex) {
        logger.error("error in savetofile", ex);
    } finally {
        try {
            writer.close();
        } catch (Exception ex) {
        }
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) PropertyException(javax.xml.bind.PropertyException) FileOutputStream(java.io.FileOutputStream) JAXBException(javax.xml.bind.JAXBException) JAXBContext(javax.xml.bind.JAXBContext) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) PropertyException(javax.xml.bind.PropertyException) JAXBException(javax.xml.bind.JAXBException) BufferedWriter(java.io.BufferedWriter)

Aggregations

BufferedWriter (java.io.BufferedWriter)4214 FileWriter (java.io.FileWriter)2181 File (java.io.File)1879 IOException (java.io.IOException)1847 OutputStreamWriter (java.io.OutputStreamWriter)1344 BufferedReader (java.io.BufferedReader)747 FileOutputStream (java.io.FileOutputStream)656 ArrayList (java.util.ArrayList)386 FileReader (java.io.FileReader)376 InputStreamReader (java.io.InputStreamReader)349 PrintWriter (java.io.PrintWriter)324 Writer (java.io.Writer)324 Test (org.junit.Test)286 FileNotFoundException (java.io.FileNotFoundException)217 OutputStream (java.io.OutputStream)213 HashMap (java.util.HashMap)200 Path (java.nio.file.Path)177 InputStream (java.io.InputStream)171 FileInputStream (java.io.FileInputStream)158 StringWriter (java.io.StringWriter)143