Example usage for java.net Socket getOutputStream

List of usage examples for java.net Socket getOutputStream

Introduction

In this page you can find the example usage for java.net Socket getOutputStream.

Prototype

public OutputStream getOutputStream() throws IOException 

Source Link

Document

Returns an output stream for this socket.

Usage

From source file:com.kyne.webby.rtk.modules.BukkitInterface.java

@SuppressWarnings("unchecked")
private <T> T sendRequestAndCastResponse(final RequestType type, final Map<String, Object> params,
        final Class<T> responseDataType) throws IOException {
    Socket clientSocket = null;
    ObjectOutputStream serverOutputStream = null;
    ObjectInputStream serverInputStream = null;

    final WebbyLocalData request = new WebbyLocalData(type, params);
    try {//from   w w w.j a v  a 2s  .  com
        clientSocket = new Socket("localhost", this.localPort);
        serverOutputStream = new ObjectOutputStream(clientSocket.getOutputStream());
        serverInputStream = new ObjectInputStream(clientSocket.getInputStream());
        serverOutputStream.writeObject(request);
        final WebbyLocalData response = (WebbyLocalData) serverInputStream.readObject();
        if (this.lastKnownStatus != ServerStatus.RESTARTING) {
            this.lastKnownStatus = ServerStatus.ON;
        }
        return (T) response.getRequestParams().get("DATA");
    } catch (final Exception e) {
        this.lastKnownStatus = ServerStatus.OFF;
        LogHelper.debug("Bukkit is offline or restarting");
        return null;
    } finally {
        IOUtils.closeQuietly(serverInputStream);
        IOUtils.closeQuietly(serverOutputStream);
        IOUtils.closeQuietly(clientSocket);
    }
}

From source file:com.supernovapps.audio.jstreamsourcer.Icecast.java

public boolean start(Socket sock) {
    try {/*from ww w. j a va2  s.  c o m*/
        this.sock = sock;
        sock.connect(new InetSocketAddress(host, port), timeout);
        sock.setSendBufferSize(64 * 1024);
        out = sock.getOutputStream();

        PrintWriter output = new PrintWriter(out);
        output.println("SOURCE " + path + " HTTP/1.0");

        writeAuthentication(output);
        writeHeaders(output);

        output.println("");
        output.flush();

        InputStreamReader isr = new InputStreamReader(sock.getInputStream());
        BufferedReader in = new BufferedReader(isr);
        String line = in.readLine();

        if (line == null || !line.contains("HTTP") || !line.contains("OK")) {
            if (listener != null) {
                listener.onError("Connection / Authentification error");
            }
            return false;
        }
    } catch (Exception e) {
        e.printStackTrace();

        try {
            if (sock != null) {
                sock.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }

        if (listener != null) {
            listener.onError("Connection / Authentification error");
        }

        return false;
    }
    started = true;

    if (listener != null) {
        listener.onConnected();
    }

    return true;
}

From source file:it.crs4.pydoop.pipes.BinaryProtocol.java

/**
 * Create a proxy object that will speak the binary protocol on a socket.
 * Upward messages are passed on the specified handler and downward
 * downward messages are public methods on this object.
 * @param sock The socket to communicate on.
 * @param handler The handler for the received messages.
 * @param key The object to read keys into.
 * @param value The object to read values into.
 * @param config The job's configuration
 * @throws IOException/*from   www  . j  ava  2 s . co  m*/
 */
public BinaryProtocol(Socket sock, UpwardProtocol<K2, V2> handler, K2 key, V2 value, JobConf config)
        throws IOException {
    OutputStream raw = sock.getOutputStream();
    // If we are debugging, save a copy of the downlink commands to a file
    if (Submitter.getKeepCommandFile(config)) {
        raw = new TeeOutputStream("downlink.data", raw);
    }
    stream = new DataOutputStream(new BufferedOutputStream(raw, BUFFER_SIZE));
    uplink = new UplinkReaderThread<K2, V2>(sock.getInputStream(), handler, key, value);
    uplink.setName("pipe-uplink-handler");
    uplink.start();
}

From source file:jeeves.utils.EMail.java

/** Sends the message to the mail server
  *///www.j av  a 2 s.  co m

public boolean send() throws IOException {
    Socket socket = new Socket(sMailServer, iPort);
    try {
        in = new BufferedReader(new InputStreamReader(new DataInputStream(socket.getInputStream()),
                Charset.forName(Jeeves.ENCODING)));
        out = new OutputStreamWriter(new DataOutputStream(socket.getOutputStream()), "ISO-8859-1");

        if (lookMailServer())
            if (sendData("2", "HELO " + InetAddress.getLocalHost().getHostName() + "\r\n"))
                if (sendData("2", "MAIL FROM: <" + sFrom + ">\r\n"))
                    if (sendData("2", "RCPT TO: <" + sTo + ">\r\n"))
                        if (sendData("354", "DATA\r\n"))
                            if (sendData("2", buildContent()))
                                if (sendData("2", "QUIT\r\n"))
                                    return true;

        sendData("2", "QUIT\r\n");
    } finally {
        IOUtils.closeQuietly(socket);
    }
    return false;
}

From source file:com.supernovapps.audio.jstreamsourcer.ShoutcastV1Test.java

@Test
public void testConnectFails() throws IOException {
    Socket mock = EasyMock.createNiceMock(Socket.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("KO").getBytes());

    EasyMock.expect(mock.getOutputStream()).andReturn(out);
    EasyMock.expect(mock.getInputStream()).andReturn(in);
    EasyMock.replay(mock);//from ww  w  . j  a v a 2 s.  c  o  m

    boolean started = shoutcast.start(mock);

    Assert.assertFalse(started);
    Assert.assertFalse(shoutcast.isStarted());
}

From source file:com.supernovapps.audio.jstreamsourcer.IcecastTest.java

@Test
public void testConnectFails() throws IOException {
    Socket mock = EasyMock.createNiceMock(Socket.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("KO").getBytes());

    EasyMock.expect(mock.getOutputStream()).andReturn(out);
    EasyMock.expect(mock.getInputStream()).andReturn(in);
    EasyMock.replay(mock);//from  w w w  .  ja  v a2 s  .  c om

    boolean started = icecast.start(mock);

    Assert.assertFalse(started);
    Assert.assertFalse(icecast.isStarted());
}

From source file:it.crs4.pydoop.mapreduce.pipes.BinaryProtocol.java

/**
 * Create a proxy object that will speak the binary protocol on a socket.
 * Upward messages are passed on the specified handler and downward
 * downward messages are public methods on this object.
 * @param sock The socket to communicate on.
 * @param handler The handler for the received messages.
 * @param key The object to read keys into.
 * @param value The object to read values into.
 * @param config The job's configuration
 * @throws IOException/*from  w w w . j  av  a 2  s  . com*/
 */
public BinaryProtocol(Socket sock, UpwardProtocol<K2, V2> handler, K2 key, V2 value, Configuration config)
        throws IOException {
    OutputStream raw = sock.getOutputStream();
    // If we are debugging, save a copy of the downlink commands to a file
    if (Submitter.getKeepCommandFile(config)) {
        raw = new TeeOutputStream("downlink.data", raw);
    }
    stream = new DataOutputStream(new BufferedOutputStream(raw, BUFFER_SIZE));
    uplink = new UplinkReaderThread<K2, V2>(sock.getInputStream(), handler, key, value);
    uplink.setName("pipe-uplink-handler");
    uplink.start();
}

From source file:net.lightbody.bmp.proxy.jetty.http.HttpTunnel.java

/** Constructor. 
 * @param socket The tunnel socket./*from w ww .  j  a va2  s .  c o  m*/
 * @param timeoutMs The maximum time to wait for a read on the tunnel. Note that
 * sotimer exceptions are ignored by the tunnel.
 * @param in Alternative input stream or null if using normal socket stream
 * @param out Alternative output stream or null if using normal socket stream
 * @param timeoutMs
 * @throws IOException 
 */
public HttpTunnel(Socket socket, InputStream in, OutputStream out) throws IOException {
    _socket = socket;
    _sIn = in;
    _sOut = out;
    if (_sIn == null)
        _sIn = _socket.getInputStream();
    if (_sOut == null)
        _sOut = socket.getOutputStream();
    _timeoutMs = 30000;
}

From source file:com.supernovapps.audio.jstreamsourcer.ShoutcastV1Test.java

@Test
public void testConnectSuccess() throws IOException {
    Socket sockMock = EasyMock.createNiceMock(Socket.class);

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    ByteArrayInputStream in = new ByteArrayInputStream(new String("HTTP OK").getBytes());

    EasyMock.expect(sockMock.getOutputStream()).andReturn(out);
    EasyMock.expect(sockMock.getInputStream()).andReturn(in);
    EasyMock.expect(sockMock.isConnected()).andReturn(true);
    EasyMock.replay(sockMock);/*  w  w  w  .j a va  2s. c  o m*/

    boolean started = shoutcast.start(sockMock);

    Assert.assertTrue(started);
    Assert.assertTrue(shoutcast.isStarted());
}

From source file:com.myJava.file.driver.remote.ftp.SecuredSocketFactory.java

private void sendCommand(String command, Socket socket, boolean readReply) throws IOException {
    Logger.defaultLogger().info("Sending FTP command : " + command);

    // Send Command
    BufferedWriter out = new BufferedWriter(
            new OutputStreamWriter(socket.getOutputStream(), client.getControlEncoding()));
    out.write(command + SocketClient.NETASCII_EOL);
    out.flush();// w  w  w .j a  v  a 2s  .c o  m

    if (readReply) {
        readReply(socket);
    }
}