Package com.google.common.io

Examples of com.google.common.io.Closer


public class JsonPrinterTest {

  @Test
  public void standardJsonPrinting() throws Exception {
    Closer closer = Closer.create();
    try {
      ByteArrayOutputStream baos = closer.register(new ByteArrayOutputStream());
      PrintStream out = closer.register(new PrintStream(baos));

      new JsonPrinter(out).print(standardProcess());
      String result = new String(baos.toByteArray());

      assertThat(result).contains("\"url\":\"service:jmx:rmi:///jndi/rmi://example.org:123/jmxrmi\"");
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      closer.close();
    }
  }
View Full Code Here


    }
  }

  @Test
  public void prettyJsonPrinting() throws Exception {
    Closer closer = Closer.create();
    try {
      ByteArrayOutputStream baos = closer.register(new ByteArrayOutputStream());
      PrintStream out = closer.register(new PrintStream(baos));

      new JsonPrinter(out).prettyPrint(standardProcess());
      String result = new String(baos.toByteArray());

      assertThat(result).contains("\"url\" : \"service:jmx:rmi:///jndi/rmi://example.org:123/jmxrmi\"");
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      closer.close();
    }
  }
View Full Code Here

      throw new IllegalStateException("Server already started");
    }
    thread = new Thread(new Runnable() {
      @Override
      public void run() {
        Closer closer = Closer.create();
        try {
          try {
            server = closer.register(new ServerSocket(0));
            while (true) {
              processRequests(server);
            }
          } catch (Throwable t) {
            throw closer.rethrow(t);
          } finally {
            closer.close();
            server = null;
          }
        } catch (IOException ioe) {
          log.error("Exception in TCP echo server", ioe);
        }
View Full Code Here

      log.error("TCP Echo server seems to take too long to start", interrupted);
    }
  }

  private void processRequests(ServerSocket server) throws IOException {
    Closer closer = Closer.create();
    try {
      Socket socket = server.accept();
      synchronized (startSynchro) {
        startSynchro.notifyAll();
      }
      BufferedReader in = closer.register(new BufferedReader(new InputStreamReader(socket.getInputStream())));
      PrintWriter out = closer.register(new PrintWriter(socket.getOutputStream()));
      String line;
      while ((line = in.readLine()) != null) {
        out.print(line);
      }
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      closer.close();
    }
  }
View Full Code Here

    echoServerAddress = echoServer.getLocalSocketAddress();
  }

  @Test
  public void createdSocketIsValid() throws IOException {
    Closer closer = Closer.create();
    try {
      SocketFactory socketFactory = new SocketFactory();
      Socket socket = closer.register(socketFactory.makeObject(echoServerAddress));
      assertThat(socketFactory.validateObject(echoServerAddress, socket)).isTrue();
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      closer.close();
    }
  }
View Full Code Here

    assertThat(new SocketFactory().validateObject(echoServerAddress, null)).isFalse();
  }

  @Test
  public void closedSocketIsInvalid() throws IOException {
    Closer closer = Closer.create();
    try {
      SocketFactory socketFactory = new SocketFactory();
      Socket socket = closer.register(socketFactory.makeObject(echoServerAddress));
      socket.close();
      assertThat(socketFactory.validateObject(echoServerAddress, socket)).isFalse();
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      closer.close();
    }
  }
View Full Code Here

  @Provides
  @Inject
  Scheduler scheduler(JmxTransConfiguration configuration, GuiceJobFactory jobFactory) throws SchedulerException, IOException {
    StdSchedulerFactory serverSchedFact = new StdSchedulerFactory();
    Closer closer = Closer.create();
    try {
      InputStream stream;
      if (configuration.getQuartPropertiesFile() == null) {
        stream = closer.register(JmxTransModule.class.getResourceAsStream("/quartz.server.properties"));
      } else {
        stream = closer.register(new FileInputStream(configuration.getQuartPropertiesFile()));
      }
      serverSchedFact.initialize(stream);
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      closer.close();
    }
    Scheduler scheduler = serverSchedFact.getScheduler();
    scheduler.setJobFactory(jobFactory);
    return scheduler;
  }
View Full Code Here

  /**
   * Create a temporary ANT file for executing JUnit4 ANT task.
   */
  private File createTemporaryAntFile(Document doc) throws IOException {
    Closer closer = Closer.create();
    try {
      File antFile = File.createTempFile("junit4-ant-", ".xml", dir);
      OutputStream os = closer.register(new FileOutputStream(antFile));
      XMLWriter xmlWriter = new XMLWriter(os, OutputFormat.createPrettyPrint());
      xmlWriter.write(doc);
      return antFile;
    } catch (Throwable t) {
      throw closer.rethrow(t);
    } finally {
      closer.close();
    }
  }
View Full Code Here

    private void checkSerializable(final Object attribute) throws IOException {
        if (!Serializable.class.isAssignableFrom(attribute.getClass())) {
            log.warn(format("Session attribute [%s] of class [%s] is not "
                    + "serializable.", attribute, attribute.getClass()));
        }
        final Closer closer = Closer.create();
        try {
            final ObjectOutputStream out = new ObjectOutputStream(
                    ByteStreams.nullOutputStream());
            out.writeObject(attribute);
        } finally {
            closer.close();
        }
    }
View Full Code Here

        return sessionSize;
    }

    private long measureSerializedSize(final Object attribute)
            throws IOException {
        final Closer closer = Closer.create();
        try {
            final CountingOutputStream countingStream = closer
                    .register(new CountingOutputStream(ByteStreams
                            .nullOutputStream()));
            final ObjectOutputStream out = closer
                    .register(new ObjectOutputStream(countingStream));
            out.writeObject(attribute);
            return countingStream.getCount();
        } finally {
            closer.close();
        }
    }
View Full Code Here

TOP

Related Classes of com.google.common.io.Closer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.