Java Code Examples for java.io.BufferedInputStream

The following code examples are extracted from open source projects. You can click to vote up the examples you like. Your votes will be used in the intelligent system to predict more good examples. Thank you for your support!

Code Example 1:

  1 
vote

From project 360-Engine-for-Android, under directory /src/com/vodafone360/people/service/transport/tcp/.

Source ResponseReaderThread.java

/**
 * Sets the input stream that will be used to read responses from.
 * 
 * @param inputStream The input stream to read from. Should not be null as
 *            this would trigger a reconnect of the whole socket and cause
 *            all transport-threads to be restarted.
 */
public void setInputStream(BufferedInputStream inputStream) {
    HttpConnectionThread.logI("RpgTcpResponseReader.setInputStream()", "Setting new IS: "
            + ((null != inputStream) ? "not null" : "null"));
    if (null != inputStream) {
        mIs = new DataInputStream(inputStream);
    } else {
        mIs = null;
    }
}
 

Code Example 2:

  1 
vote

From project 360-Engine-for-Android, under directory /src/com/vodafone360/people/service/transport/tcp/.

Source TcpConnectionThread.java

/**
 * Attempts to reconnect the socket if it has been closed for some reason.
 * 
 * @throws IOException Thrown if something goes wrong while reconnecting the
 *             socket.
 */
private void reconnectSocket() throws IOException {
    HttpConnectionThread.logI("TcpConnectionThread.reconnectSocket()",
            "Reconnecting Socket on " + mRpgTcpUrl + ":" + mRpgTcpPort);
    mSocket = null;
    mSocket = new Socket();
    mSocket.connect(new InetSocketAddress(mRpgTcpUrl, mRpgTcpPort), TCP_DEFAULT_TIMEOUT);

    mBufferedInputStream = new BufferedInputStream(mSocket.getInputStream());
    mOs = mSocket.getOutputStream();
    HttpConnectionThread.logI("TcpConnectionThread.reconnectSocket()", "Socket started: "
            + mRpgTcpUrl + ":" + mRpgTcpPort);
}
 

Code Example 3:

  1 
vote

From project 360-Engine-for-Android, under directory /tests/src/com/vodafone360/people/tests/service/transport/tcp/conn_less/respreader/.

Source ResponseReaderThreadTest.java

@Suppress
@MediumTest
public void testStartConnection() {
	DecoderThread decoder = new DecoderThread();
	MockTcpConnectionThread mockThread = new MockTcpConnectionThread(decoder, null);
	MockResponseReaderThread respReader = new MockResponseReaderThread(mockThread, decoder, 
			null // QUICKFIX: Not sure about this value 
			);
	MockOTAInputStream mIs = new MockOTAInputStream(
			new ByteArrayInputStream(new byte[] {1, 2, 3, 4, 5}));
	respReader.setInputStream(new BufferedInputStream(mIs));

	assertNull(respReader.getConnectionThread());

	respReader.startConnection();
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {}

	assertNotNull(respReader.getConnectionThread());
	if (null != respReader.getConnectionThread()) {
		assertTrue(respReader.getConnectionThread().isAlive());
	}
	assertTrue(respReader.getIsConnectionRunning());
}
 

Code Example 4:

  1 
vote

From project 360-Engine-for-Android, under directory /tests/src/com/vodafone360/people/tests/service/transport/tcp/conn_less/respreader/.

Source ResponseReaderThreadTest.java

@Suppress
@MediumTest
public void testStopConnection() {
	DecoderThread decoder = new DecoderThread();
	MockTcpConnectionThread mockThread = new MockTcpConnectionThread(decoder, null);
	MockResponseReaderThread respReader = new MockResponseReaderThread(mockThread, decoder,
			null // QUICKFIX: Not sure about this value
			);
	MockOTAInputStream mIs = new MockOTAInputStream(
			new ByteArrayInputStream(new byte[] {1, 2, 3, 4, 5}));
	respReader.setInputStream(new BufferedInputStream(mIs));

	respReader.startConnection();
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {}

	Thread t = respReader.getConnectionThread();
	assertNotNull(t);
	if (null != t) {
		assertTrue(t.isAlive());
		assertTrue(respReader.getIsConnectionRunning());
	}

	// now comes the actual test
	respReader.stopConnection();
	try {
		Thread.sleep(1000);
	} catch (InterruptedException e) {}

	assertNull(respReader.getInputStream());
	assertNull(respReader.getConnectionThread());
	assertFalse(respReader.getIsConnectionRunning());
}
 

Code Example 5:

  1 
vote

From project 360-Engine-for-Android, under directory /tests/src/com/vodafone360/people/tests/service/transport/tcp/conn_less/respreader/.

Source ResponseReaderThreadTest.java

@MediumTest
public void testSetInputstream() {
	DecoderThread decoder = new DecoderThread();
	MockTcpConnectionThread mockThread = new MockTcpConnectionThread(decoder, null);
	MockResponseReaderThread respReader = new MockResponseReaderThread(mockThread, decoder,
			null // QUICKFIX: Not sure about this value
			);
	ByteArrayInputStream bais = new ByteArrayInputStream(new byte[] {1, 2, 3, 4, 5});

	respReader.setInputStream(new BufferedInputStream(bais));
	DataInputStream dis = (DataInputStream) respReader.getInputStream();
	assertNotNull(dis);

	// let's test all the bytes...
	if (null != dis) {
		boolean areBytesCorrect = true;

		for (int i = 1; i < 6; i++) {
			try {
				int j = dis.readByte();
				if (-1 == j) {
					fail("Unexpected end of the DataInputStream");
					areBytesCorrect = false;
				} else if (i != j) {
					fail("Characters differ: i: " + i + " vs. j: " + j);
					areBytesCorrect = false;
				}
			} catch (IOException e) {}
		}

		assertTrue(areBytesCorrect);
	}

	respReader.setInputStream(null);
	assertNull(respReader.getInputStream());
}
 

Code Example 6:

  1 
vote

From project 3Dto2DApplet, under directory /src/java/nl/dannyarends/generic/.

Source FileUtils.java

public static void unJar(String path, String to,boolean verbose){
	File file = new File(path);
	Utils.console("Starting unjar of: "+ file.getAbsolutePath() + File.separator + "*.jar");
	if( file.exists() ) {
	  File[] files;
	  if(file.isFile()){
	     files = new File[]{file};
	  }else{
	     files = file.listFiles();
	  }
		for(int i=0; i<files.length; i++) {
			if(!files[i].isDirectory() && files[i].getName().endsWith("jar")) {
				Utils.console("unJar file: "+ files[i].getName() + " (" + (i+1) + "/" + files.length +")");
				try {
					FileInputStream fis =  new FileInputStream(files[i]);
					JarInputStream zis = new JarInputStream(new BufferedInputStream(fis));
					ZipEntry entry;
					while((entry = zis.getNextEntry()) != null){
						writejarEntry(zis,entry,to,false);
					}
					zis.close();
				} catch (IOException e) {
					Utils.log("unJar error:",e);
				}
			}
		}
	}else{
		System.err.println("Failed to create: " + file.getAbsolutePath());
	}
}
 

Code Example 7:

  1 
vote

From project 3Dto2DApplet, under directory /src/java/nl/dannyarends/generic/.

Source UniversalLoader.java

byte[] bytearrayfromfile(File f) throws IOException{
	int filesize = (int) f.length();
	byte[] buffer = new byte[filesize];
	BufferedInputStream instream = new BufferedInputStream(new FileInputStream(f));
	for(int i=0;i < filesize; i++){
		buffer[i] = (byte) instream.read();
		if(i%1024==0)System.out.print(".");
	}
	return buffer;
}
 

Code Example 8:

  1 
vote

From project aboutsip, under directory /modules/yajpcap/src/main/java/com/aboutsip/yajpcap/framer/layer1/.

Source PcapFramer.java

/**
 * Frame the pcap frame. This frame is the entry into the pcap and we will
 * always frame a pcap one before asking it to frame the rest
 * 
 * @param byteOrder
 * @param in
 * @return the framed PcapFrame or null if nothing left to frame in the
 *         stream
 * @throws IOException
 */
public PcapFrame frame(final ByteOrder byteOrder, final BufferedInputStream in) throws IOException {
    // not enough bytes in the stream
    final int l = in.available();
    if (l == -1) {
        return null;
    }

    if (l < 16) {
        return null;
    }

    final byte[] record = new byte[16];
    in.read(record);
    return null;
}
 

Code Example 9:

  1 
vote

From project acceleo, under directory /plugins/org.eclipse.acceleo.common/src/org/eclipse/acceleo/common/internal/utils/workspace/.

Source AcceleoModelManager.java

/**
 * Reads the state of the project.
 * 
 * @param project
 *            The given project
 * @return The state of the project
 */
private AcceleoProjectState readBuiltState(IProject project) {
	File file = this.getSerializationFile(project);
	if (file != null && file.exists()) {
		try {
			DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
			try {
				String pluginID = in.readUTF();
				if (!pluginID.equals(AcceleoCommonPlugin.PLUGIN_ID)) {
					AcceleoCommonPlugin.log(AcceleoCommonMessages
							.getString("AcceleoModelManager.WrongFileFormat"), true); //$NON-NLS-1$
				}
				String kind = in.readUTF();
				if (!"STATE".equals(kind)) { //$NON-NLS-1$
					AcceleoCommonPlugin.log(AcceleoCommonMessages
							.getString("AcceleoModelManager.WrongFileFormat"), true); //$NON-NLS-1$
				}
				if (in.readBoolean()) {
					return AcceleoModelManager.readState(project, in);
				}
			} finally {
				in.close();
			}
			// CHECKSTYLE:OFF
		} catch (Exception e) {
			// CHECKSTYLE:ON
			AcceleoCommonPlugin.log(e, true);
		}
	}
	return null;
}
 

Code Example 10:

  1 
vote

From project acceleo, under directory /tests/org.eclipse.acceleo.common.tests/src/org/eclipse/acceleo/common/tests/unit/utils/modelutils/.

Source LoadFromInputStreamTest.java

/**
 * Tests {@link ModelUtils#load(InputStream, String, ResourceSet)} with valid inputStream and resourceSet.
 * As the filename given is invalid, expects the model to be loaded as an XMI file.
 * 
 * @throws IOException
 *             Allows us not to catch it. Test just fails if thrown.
 */
public void testLoadModelFromValidInputInvalidExtension() throws IOException {
	for (File model : models) {
		try {
			for (String invalidFileName : INVALID_FILENAMES) {
				final FileInputStream fsInput = new FileInputStream(model);
				final BufferedInputStream buffInput = new BufferedInputStream(fsInput);
				final ResourceSet resourceSet = new ResourceSetImpl();
				final EObject result = ModelUtils.load(buffInput, invalidFileName, resourceSet);

				assertNotNull("ModelUtils didn't load its target model.", result);
				assertEquals("Loaded object was associated to an unexpected resourceSet.", resourceSet,
						result.eResource().getResourceSet());
				assertTrue("Model loaded as an unexpected type of resource.",
						result.eResource() instanceof XMIResource);

				buffInput.close();
				fsInput.close();
			}
		} catch (FileNotFoundException e) {
			fail(MESSAGE_FILENOTFOUNDEXCEPTION_UNEXPECTED);
		}
	}
}
 

Code Example 11:

  1 
vote

From project acceleo, under directory /tests/org.eclipse.acceleo.common.tests/src/org/eclipse/acceleo/common/tests/unit/utils/modelutils/.

Source LoadFromInputStreamTest.java

/**
 * Tests {@link ModelUtils#load(InputStream, String, ResourceSet)} with valid inputStream and resourceSet.
 * As the filename given is valid, expects the model to be loaded in an appropriate resource.
 * 
 * @throws IOException
 *             Allows us not to catch it. Test just fails if thrown.
 */
public void testLoadModelFromValidInputValidExtension() throws IOException {
	for (File model : models) {
		try {
			final FileInputStream fsInput = new FileInputStream(model);
			final BufferedInputStream buffInput = new BufferedInputStream(fsInput);
			final ResourceSet resourceSet = new ResourceSetImpl();
			final EObject result = ModelUtils.load(buffInput, model.getName(), resourceSet);

			assertNotNull("ModelUtils didn't load its target model.", result);
			assertEquals("Loaded object was associated to an unexpected resourceSet.", resourceSet,
					result.eResource().getResourceSet());

			buffInput.close();
			fsInput.close();
		} catch (FileNotFoundException e) {
			fail(MESSAGE_FILENOTFOUNDEXCEPTION_UNEXPECTED);
		}
	}
}
 

Code Example 12:

  1 
vote

From project accumulo, under directory /core/src/main/java/org/apache/accumulo/core/file/rfile/bcfile/.

Source Compression.java

@Override
public synchronized InputStream createDecompressionStream(InputStream downStream, Decompressor decompressor, int downStreamBufferSize) throws IOException {
  if (!isSupported()) {
    throw new IOException("LZO codec class not specified. Did you forget to set property " + CONF_LZO_CLASS + "?");
  }
  InputStream bis1 = null;
  if (downStreamBufferSize > 0) {
    bis1 = new BufferedInputStream(downStream, downStreamBufferSize);
  } else {
    bis1 = downStream;
  }
  conf.setInt("io.compression.codec.lzo.buffersize", 64 * 1024);
  CompressionInputStream cis = codec.createInputStream(bis1, decompressor);
  BufferedInputStream bis2 = new BufferedInputStream(cis, DATA_IBUF_SIZE);
  return bis2;
}
 

Code Example 13:

  1 
vote

From project accumulo, under directory /core/src/main/java/org/apache/accumulo/core/file/rfile/bcfile/.

Source Compression.java

@Override
public synchronized InputStream createDecompressionStream(InputStream downStream, Decompressor decompressor, int downStreamBufferSize) throws IOException {
  // Set the internal buffer size to read from down stream.
  if (downStreamBufferSize > 0) {
    codec.getConf().setInt("io.file.buffer.size", downStreamBufferSize);
  }
  CompressionInputStream cis = codec.createInputStream(downStream, decompressor);
  BufferedInputStream bis2 = new BufferedInputStream(cis, DATA_IBUF_SIZE);
  return bis2;
}
 

Code Example 14:

  1 
vote

From project accumulo, under directory /core/src/main/java/org/apache/accumulo/core/file/rfile/bcfile/.

Source Compression.java

@Override
public synchronized InputStream createDecompressionStream(InputStream downStream, Decompressor decompressor, int downStreamBufferSize) throws IOException {
  if (!isSupported()) {
    throw new IOException("SNAPPY codec class not specified. Did you forget to set property " + CONF_SNAPPY_CLASS + "?");
  }
  if (downStreamBufferSize > 0) {
    conf.setInt("io.file.buffer.size", downStreamBufferSize);
  }
  CompressionInputStream cis = snappyCodec.createInputStream(downStream, decompressor);
  BufferedInputStream bis2 = new BufferedInputStream(cis, DATA_IBUF_SIZE);
  return bis2;
}
 

Code Example 15:

  1 
vote

From project accumulo, under directory /core/src/main/java/org/apache/accumulo/core/util/.

Source TTimeoutTransport.java

public static TTransport create(SocketAddress addr, long timeoutMillis) throws IOException {
  Socket socket = SelectorProvider.provider().openSocketChannel().socket();
  socket.setSoLinger(false, 0);
  socket.setTcpNoDelay(true);
  socket.connect(addr);
  InputStream input = new BufferedInputStream(new SocketInputStream(socket, timeoutMillis), 1024 * 10);
  OutputStream output = new BufferedOutputStream(new SocketOutputStream(socket, timeoutMillis), 1024 * 10);
  return new TIOStreamTransport(input, output);
}
 

Code Example 16:

  1 
vote

From project activemq, under directory /activemq-core/src/main/java/org/apache/activemq/command/.

Source ActiveMQStreamMessage.java

private void initializeReading() throws MessageNotReadableException {
    checkWriteOnlyBody();
    if (this.dataIn == null) {
        ByteSequence data = getContent();
        if (data == null) {
            data = new ByteSequence(new byte[] {}, 0, 0);
        }
        InputStream is = new ByteArrayInputStream(data);
        if (isCompressed()) {
            is = new InflaterInputStream(is);
            is = new BufferedInputStream(is);
        }
        this.dataIn = new DataInputStream(is);
    }
}
 

Code Example 17:

  1 
vote

From project activemq, under directory /activemq-core/src/main/java/org/apache/activemq/util/.

Source FactoryFinder.java

static public Properties loadProperties(String uri) throws IOException {
    // lets try the thread context class loader first
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader == null) {
        classLoader = StandaloneObjectFactory.class.getClassLoader();
    }
    InputStream in = classLoader.getResourceAsStream(uri);
    if (in == null) {
        in = FactoryFinder.class.getClassLoader().getResourceAsStream(uri);
        if (in == null) {
            throw new IOException("Could not find factory class for resource: " + uri);
        }
    }

    // lets load the file
    BufferedInputStream reader = null;
    try {
        reader = new BufferedInputStream(in);
        Properties properties = new Properties();
        properties.load(reader);
        return properties;
    } finally {
        try {
            reader.close();
        } catch (Exception e) {
        }
    }
}
 

Code Example 18:

  1 
vote

From project activemq, under directory /activemq-core/src/main/java/org/apache/activemq/util/.

Source LogWriterFinder.java

/**
 * Given a log writer name, returns a Properties object with a "class" property
 * whose value is a String with the name of the class to be loaded.
 * @param logWriterName a log writer name.
 * @return a Properties object with a "class" property
 * @throws IOException
 */
protected Properties doFindLogWriterProperties (String logWriterName) throws IOException {

    String uri = path + logWriterName;

    // lets try the thread context class loader first
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader == null) classLoader = getClass().getClassLoader();
    InputStream in = classLoader.getResourceAsStream(uri);
    if (in == null) {
        in = LogWriterFinder.class.getClassLoader().getResourceAsStream(uri);
        if (in == null) {
            log.error("Could not find log writer for resource: " + uri);
            throw new IOException("Could not find log writer for resource: " + uri);
        }
    }

    // lets load the file
    BufferedInputStream reader = null;
    Properties properties = new Properties();
    try {
        reader = new BufferedInputStream(in);
        properties.load(reader);
        return properties;
    } finally {
        try {
            reader.close();
        } catch (Exception e) {
        }
    }
}
 

Code Example 19:

  1 
vote

From project acube, under directory /ACube/src/acube/.

Source Tools.java

public static byte[] loadTable(final String key, final int[] sizes) {
  InputStream in = null;
  try {
    final String fileName = CACHE_DIR + File.separator + "prune-" + key + ".acube";
    final File file = new File(fileName);
    if (file.exists() && file.isFile() && file.length() > 8) {
      int size = (int)file.length() - 8;
      in = new BufferedInputStream(new FileInputStream(file));
      final byte[] tab = new byte[size];
      Arrays.fill(sizes, 0);
      for (int i = 0; i < 2; i++)
        for (int j = 0; j < 4; j++)
          sizes[i] = sizes[i] << 8 | in.read();
      while (size > 0) {
        final int read = in.read(tab, tab.length - size, size);
        if (read == -1)
          return null;
        size -= read;
      }
      return tab;
    }
  } catch (final Exception e) {} finally {
    try {
      if (in != null)
        in.close();
    } catch (final IOException ee) {}
  }
  return null;
}
 

Code Example 20:

  1 
vote

From project acube, under directory /ACube/src/acube/.

Source Tools.java

public static boolean loadTable(final short[][] table, final String key) {
  InputStream in = null;
  try {
    final String fileName = CACHE_DIR + File.separator + "turns-" + key + ".acube";
    final File file = new File(fileName);
    if (file.exists() && file.isFile() && file.length() > table.length * 4) {
      Arrays.fill(table, null);
      in = new BufferedInputStream(new FileInputStream(file));
      for (int i = 0; i < table.length; i++) {
        int size = 0;
        for (int j = 0; j < 4; j++)
          size = size << 8 | in.read();
        if (size > 0) {
          table[i] = new short[size];
          for (int ii = 0; ii < size; ii++) {
            int val = 0;
            for (int j = 0; j < 2; j++) {
              final int c = in.read();
              if (c < 0)
                return false;
              val = val << 8 | c & 255;
            }
            table[i][ii] = (short)val;
          }
        }
      }
      return true;
    }
  } catch (final Exception e) {} finally {
    try {
      if (in != null)
        in.close();
    } catch (final IOException ee) {}
  }
  return false;
}
 

Code Example 21:

  1 
vote

From project acube, under directory /ACube/src/acube/.

Source Tools.java

public static boolean loadTable(final int[][] table, final String key) {
  InputStream in = null;
  try {
    final String fileName = CACHE_DIR + File.separator + "turns-" + key + ".acube";
    final File file = new File(fileName);
    if (file.exists() && file.isFile() && file.length() > table.length * 4) {
      Arrays.fill(table, null);
      in = new BufferedInputStream(new FileInputStream(file));
      for (int i = 0; i < table.length; i++) {
        int size = 0;
        for (int j = 0; j < 4; j++)
          size = size << 8 | in.read();
        if (size > 0) {
          table[i] = new int[size];
          for (int ii = 0; ii < size; ii++) {
            int val = 0;
            for (int j = 0; j < 4; j++) {
              final int c = in.read();
              if (c < 0)
                return false;
              val = val << 8 | c & 255;
            }
            table[i][ii] = val;
          }
        }
      }
      return true;
    }
  } catch (final Exception e) {} finally {
    try {
      if (in != null)
        in.close();
    } catch (final IOException ee) {}
  }
  return false;
}