List of usage examples for java.io InputStream InputStream
InputStream
From source file:Alias2.java
public TestStream(String className) { super(System.out, true); // Autoflush System.setOut(this); System.setErr(this); stdin = System.in; // Save to restore in dispose() // Replace the default version with one that // automatically produces input on demand: System.setIn(new BufferedInputStream(new InputStream() { char[] input = ("test\n").toCharArray(); int index = 0; public int read() { return (int) input[index = (index + 1) % input.length]; }// w w w . java 2 s. c om })); this.className = className; openOutputFile(); }
From source file:org.jenkinsci.plugins.fabric8.support.hack.LargeText.java
/** * Returns {@link Reader} for reading the raw bytes. *//*w w w. j av a2 s . co m*/ public Reader readAll() throws IOException { return new InputStreamReader(new InputStream() { final Session session = source.open(); public int read() throws IOException { byte[] buf = new byte[1]; int n = session.read(buf); if (n == 1) return buf[0]; else return -1; // EOF } public int read(byte[] buf, int off, int len) throws IOException { return session.read(buf, off, len); } public void close() throws IOException { session.close(); } }, charset); }
From source file:org.apache.lucene.replicator.http.HttpClientBase.java
/** * Internal utility: input stream of the provided response, which optionally * consumes the response's resources when the input stream is exhausted. *//* w w w . ja v a 2 s .c o m*/ public InputStream responseInputStream(HttpResponse response, boolean consume) throws IOException { final HttpEntity entity = response.getEntity(); final InputStream in = entity.getContent(); if (!consume) { return in; } return new InputStream() { private boolean consumed = false; @Override public int read() throws IOException { final int res = in.read(); consume(res); return res; } @Override public void close() throws IOException { in.close(); consume(-1); } @Override public int read(byte[] b) throws IOException { final int res = in.read(b); consume(res); return res; } @Override public int read(byte[] b, int off, int len) throws IOException { final int res = in.read(b, off, len); consume(res); return res; } private void consume(int minusOne) { if (!consumed && minusOne == -1) { try { EntityUtils.consume(entity); } catch (Exception e) { // ignored on purpose } consumed = true; } } }; }
From source file:org.talend.mdm.bulkload.client.BulkloadClientTest.java
public void testInterruptedBulkLoadOrder() throws Exception { String serverURL = "http://localhost:8180/talendmdm/services/bulkload"; boolean isServerRunning = isServerRunning(serverURL); if (isServerRunning) { BulkloadClient client = new BulkloadClient(serverURL, "administrator", "administrator", null, "Product", "Product", "Product"); client.setOptions(new BulkloadOptions()); int count = 5; for (int i = 0; i < count; i++) { InputStreamMerger manager = client.load(); synchronized (manager) { InputStream bin = new InputStream() { @Override/*from www . ja va 2 s. c om*/ public synchronized int read() throws IOException { try { Thread.sleep(500); } catch (InterruptedException e) { throw new RuntimeException(e); } return -1; } }; manager.push(bin); manager.close(); while (!manager.isAlreadyProcessed()) { manager.wait(100); } assertEquals(true, manager.isAlreadyProcessed()); manager.notify(); } } } }
From source file:org.openehealth.ipf.commons.lbs.store.LargeBinaryStoreTest.java
@Test(expected = ResourceIOException.class) public void testAddWithCorruptedInputStream() throws Exception { InputStream inputStream = new InputStream() { @Override/* w ww . jav a 2 s . co m*/ public int read() throws IOException { throw new IOException("for testing"); } }; store.add(inputStream); }
From source file:org.metaeffekt.dita.maven.glossary.GlossaryMapCreator.java
protected Document readDocument(File file) { SAXReader reader = new SAXReader(); reader.setValidation(false);//from w w w.j av a2 s .c o m reader.setIncludeInternalDTDDeclarations(false); reader.setIncludeExternalDTDDeclarations(false); reader.setIgnoreComments(true); reader.setEntityResolver(new EntityResolver() { @Override public InputSource resolveEntity(String arg0, String arg1) throws SAXException, IOException { return new InputSource(new InputStream() { @Override public int read() throws IOException { return -1; } }); } }); try { return reader.read(file); } catch (DocumentException e) { return null; } }
From source file:es.uvigo.ei.sing.jarvest.core.HTTPUtils.java
public synchronized static InputStream doPost(String urlstring, String queryString, String separator, Map<String, String> additionalHeaders, StringBuffer charsetb) throws HttpException, IOException { System.err.println("posting to: " + urlstring + ". query string: " + queryString); HashMap<String, String> query = parseQueryString(queryString, separator); HttpClient client = getClient();// www . ja v a2 s . co m URL url = new URL(urlstring); int port = url.getPort(); if (port == -1) { if (url.getProtocol().equalsIgnoreCase("http")) { port = 80; } if (url.getProtocol().equalsIgnoreCase("https")) { port = 443; } } client.getHostConfiguration().setHost(url.getHost(), port, url.getProtocol()); client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); final PostMethod post = new PostMethod(url.getFile()); addHeaders(additionalHeaders, post); post.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); post.setRequestHeader("Accept", "*/*"); // Prepare login parameters NameValuePair[] valuePairs = new NameValuePair[query.size()]; int counter = 0; for (String key : query.keySet()) { //System.out.println("Adding pair: "+key+": "+query.get(key)); valuePairs[counter++] = new NameValuePair(key, query.get(key)); } post.setRequestBody(valuePairs); //authpost.setRequestEntity(new StringRequestEntity(requestEntity)); client.executeMethod(post); int statuscode = post.getStatusCode(); InputStream toret = null; if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) || (statuscode == HttpStatus.SC_MOVED_PERMANENTLY) || (statuscode == HttpStatus.SC_SEE_OTHER) || (statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) { Header header = post.getResponseHeader("location"); if (header != null) { String newuri = header.getValue(); if ((newuri == null) || (newuri.equals(""))) { newuri = "/"; } } else { System.out.println("Invalid redirect"); System.exit(1); } } else { charsetb.append(post.getResponseCharSet()); final InputStream in = post.getResponseBodyAsStream(); toret = new InputStream() { @Override public int read() throws IOException { return in.read(); } @Override public void close() { post.releaseConnection(); } }; } return toret; }
From source file:com.atomicleopard.thundr.ftp.FtpSession.java
public InputStream getFile(final String filename) { return timeLogAndCatch("Get file stream " + filename, new Callable<InputStream>() { @Override/* ww w . j av a 2 s . co m*/ public InputStream call() throws Exception { final InputStream is = preparedClient.retrieveFileStream(filename); int reply = preparedClient.getReplyCode(); if (is == null || (!FTPReply.isPositivePreliminary(reply) && !FTPReply.isPositiveCompletion(reply))) { throw new FtpException("Failed to open input stream: %s", preparedClient.getReplyString()); } return new InputStream() { @Override public int read() throws IOException { return is.read(); } @Override public int read(byte[] b) throws IOException { return is.read(b); } @Override public int read(byte[] b, int off, int len) throws IOException { return is.read(b, off, len); } @Override public void close() throws IOException { is.close(); preparedClient.completePendingCommand(); } }; } }); }
From source file:com.socialize.test.unit.DefaultSocializeProviderTest.java
public void testAuthenticate() throws Exception { final String key = "foo"; final String secret = "bar"; final String uuid = "uuid"; final String endpoint = "foobar/"; final String host = "host"; final String oauth_token = "oauth_token"; final String oauth_token_secret = "oauth_token_secret"; final String url = "https://" + host + "/" + endpoint; final InputStream in = new InputStream() { @Override//www .j a v a2s . c o m public int read() throws IOException { return -1; } }; AndroidMock.expect(clientFactory.isDestroyed()).andReturn(false); AndroidMock.expect(authProviderDataFactory.getBean()).andReturn(authProviderData); AndroidMock.expect(sessionFactory.create(key, secret, authProviderData)).andReturn(session); AndroidMock.expect(clientFactory.getClient()).andReturn(client); AndroidMock.expect(client.execute(request)).andReturn(response); AndroidMock.expect(response.getEntity()).andReturn(entity); AndroidMock.expect(entity.getContent()).andReturn(in); AndroidMock.expect(ioUtils.readSafe(in)).andReturn(null); AndroidMock.expect(requestFactory.getAuthRequest(session, url, uuid, authProviderData)).andReturn(request); AndroidMock.expect(jsonParser.parseObject((String) null)).andReturn(json); AndroidMock.expect(json.getJSONObject("user")).andReturn(json); AndroidMock.expect(json.getString("oauth_token")).andReturn(oauth_token); AndroidMock.expect(json.getString("oauth_token_secret")).andReturn(oauth_token_secret); AndroidMock.expect(userFactory.fromJSON(json)).andReturn(user); AndroidMock.expect(httpUtils.isHttpError(response)).andReturn(false); AndroidMock.expect(sessionPersister.load(mockContext)).andReturn(null); AndroidMock.expect(authProviderInfoBuilder.getFactory(AuthProviderType.SOCIALIZE)) .andReturn(authProviderInfoFactory); AndroidMock.expect(authProviderInfoFactory.getInstanceForRead()).andReturn(authProviderInfo); authProviderData.setAuthProviderInfo(authProviderInfo); AndroidMock.expect(session.getHost()).andReturn(host); AndroidMock.expect(authProviderData.getAuthProviderInfo()).andReturn(null); // Expect save sessionPersister.save(mockContext, session); session.setConsumerToken(oauth_token); session.setConsumerTokenSecret(oauth_token_secret); session.setUser(user); entity.consumeContent(); DefaultSocializeProvider<SocializeObject> provider = getNewProvider(); AndroidMock.replay(authProviderData, authProviderDataFactory, user, session, sessionFactory, clientFactory, requestFactory, jsonParser, client, json, userFactory, entity, response, httpUtils, sessionPersister, authProviderInfoBuilder, authProviderInfoFactory, authProviderInfo); provider.setSessionPersister(sessionPersister); provider.authenticate(endpoint, key, secret, uuid); AndroidMock.verify(authProviderData, authProviderDataFactory, user, session, sessionFactory, clientFactory, requestFactory, jsonParser, client, json, userFactory, entity, response, httpUtils, sessionPersister, authProviderInfoBuilder, authProviderInfoFactory, authProviderInfo); }
From source file:com.glaf.core.util.ByteBufferUtils.java
public static InputStream inputStream(ByteBuffer bytes) { final ByteBuffer copy = bytes.duplicate(); return new InputStream() { public int read() throws IOException { if (!copy.hasRemaining()) return -1; return copy.get() & 0xFF; }/* w w w. java 2s .co m*/ @Override public int read(byte[] bytes, int off, int len) throws IOException { if (!copy.hasRemaining()) return -1; len = Math.min(len, copy.remaining()); copy.get(bytes, off, len); return len; } @Override public int available() throws IOException { return copy.remaining(); } }; }