protected void doService(Socket s) throws IOException {
   InputStream is = s.getInputStream();
   InputStreamReader isr = new InputStreamReader(is);
   BufferedReader br = new BufferedReader(isr);
   String message = br.readLine();
   OutputStream os = s.getOutputStream();
   os.write(message.getBytes());
   os.flush();
 }
 @Test
 public void canEcho() throws Exception {
   server.start();
   Thread.yield();
   Socket s = new Socket("localhost", port);
   OutputStream os = s.getOutputStream();
   os.write("echo\n".getBytes());
   String response = new BufferedReader(new InputStreamReader(s.getInputStream())).readLine();
   assertEquals("echo", response);
 }
  private void runStreamTest(final int pLength) throws Exception {
    byte[] data = createData(pLength);
    ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
    OutputStream out = new EncoderStream(outBytes, createEncoder(), true);

    try {
      // Provoke failure for encoders that doesn't take array offset properly into account
      int off = (data.length + 1) / 2;
      out.write(data, 0, off);
      if (data.length > off) {
        out.write(data, off, data.length - off);
      }
    } finally {
      out.close();
    }

    byte[] encoded = outBytes.toByteArray();

    //        System.err.println("encoded.length: " + encoded.length);
    //        System.err.println("encoded: " + Arrays.toString(encoded));

    byte[] decoded =
        FileUtil.read(
            new DecoderStream(new ByteArrayInputStream(encoded), createCompatibleDecoder()));
    assertTrue(Arrays.equals(data, decoded));

    InputStream in =
        new DecoderStream(new ByteArrayInputStream(encoded), createCompatibleDecoder());
    outBytes = new ByteArrayOutputStream();

    try {
      FileUtil.copy(in, outBytes);
    } finally {
      outBytes.close();
      in.close();
    }

    decoded = outBytes.toByteArray();
    assertTrue(Arrays.equals(data, decoded));
  }
  public static long copy(InputStream is, OutputStream os) {
    byte[] buf = new byte[BUFFER_SIZE];
    long total = 0;
    int len = 0;
    try {
      while (-1 != (len = is.read(buf))) {
        // System.out.print(buf);
        os.write(buf, 0, len);
        total += len;
      }
    } catch (IOException ioe) {
      throw new RuntimeException("error reading stream", ioe);
    }

    return total;
  }
Ejemplo n.º 5
0
  @Before
  public void setUp() throws Exception {
    _rgchPwTest = new char[] {'p', 'a', 's', 's', 'w', 'o', 'r', 'd'};

    _fileExisting = File.createTempFile("obzvaultexisting", ".vault");
    _strDecryptedExisting = "existing";
    OBZVaultDocument doc = new OBZVaultDocument();
    doc.setAppVersion("X.Y.DEV");
    doc.setText(_strDecryptedExisting);
    doc.saveAsVaultDoc(_fileExisting, _rgchPwTest);

    _fileText = File.createTempFile("obzvaulttext", ".txt");
    _strText = "Hello world!";
    OutputStream os = new FileOutputStream(_fileText);
    os.write(_strText.getBytes());
    os.close();
  }