// Test the connected case to see if PUE is thrown public static void test2() throws Exception { setup(); server.configureBlocking(true); server.connect(isa); server.configureBlocking(false); outBuf.rewind(); server.write(outBuf); server.receive(inBuf); client.close(); Thread.sleep(2000); outBuf.rewind(); try { server.write(outBuf); Thread.sleep(2000); inBuf.clear(); server.read(inBuf); if (onSolarisOrLinux()) throw new Exception("Expected PUE not thrown"); } catch (PortUnreachableException pue) { System.err.println("received PUE"); } server.close(); }
/* * Creates a file at filename if file doesn't exist. Writes * value to that file using FileChannel. */ public static void writeToFile(File filename, String value, String hideCommand) throws IOException, InterruptedException { if (!hideCommand.trim().equals("")) { // unhideFile(filename); } if (!filename.exists()) { filename.createNewFile(); } byte[] bytes = value.getBytes(); ByteBuffer buffer = ByteBuffer.allocate(bytes.length); for (int i = 0; i < bytes.length; i++) { buffer.put(bytes[i]); } buffer.rewind(); try { fileWriter = new FileOutputStream(filename).getChannel(); fileWriter.write(buffer); } finally { fileWriter.close(); } if (!hideCommand.trim().equals("")) { // hideFile(filename); } }
public RandomAccessFile delete(RandomAccessFile raf, RandomAccessFile tempRaf) throws IOException { FileChannel fc = raf.getChannel(); fc.position(0); if (!tagExists(fc)) return raf; fc.position(6); ByteBuffer b = ByteBuffer.allocate(4); fc.read(b); b.rewind(); int tagSize = (b.get() & 0xFF) << 21; tagSize += (b.get() & 0xFF) << 14; tagSize += (b.get() & 0xFF) << 7; tagSize += b.get() & 0xFF; FileChannel tempFC = tempRaf.getChannel(); tempFC.position(0); fc.position(tagSize + 10); // Here we will try to skip eventual trash afer the tag and before the // audio data b = ByteBuffer.allocate(4); int skip = 0; while (fc.read(b) != -1) { if ((b.get(0) & 0xFF) == 0xFF && (b.get(1) & 0xE0) == 0xE0 && (b.get(1) & 0x06) != 0 && (b.get(2) & 0xF0) != 0xF0 && (b.get(2) & 0x08) != 0x08) { fc.position(fc.position() - 4); break; } fc.position(fc.position() - 3); b.rewind(); skip++; } tempFC.transferFrom(fc, 0, fc.size() - tagSize - 10 - skip); return tempRaf; }
public static void main(String[] args) throws Exception { outBuf.put("Blah Blah".getBytes()); outBuf.flip(); test1(); // This test has been disabled because there are many circumstances // under which no ICMP port unreachable packets are received // See http://java.sun.com/j2se/1.4/networking-relnotes.html if ((args.length > 0) && (args[0].equals("test2"))) { outBuf.rewind(); test2(); } }
// Since this is not connected no PortUnreachableException should be thrown public static void test1() throws Exception { setup(); server.send(outBuf, isa); server.receive(inBuf); client.close(); outBuf.rewind(); server.send(outBuf, isa); server.receive(inBuf); server.close(); }
public void write(FileOutputStream fos) throws IOException { FileChannel chan = fos.getChannel(); // Create ByteBuffer for header in case the start of our // ByteBuffer isn't actually memory-mapped ByteBuffer hdr = ByteBuffer.allocate(Header.writtenSize()); hdr.order(ByteOrder.LITTLE_ENDIAN); header.write(hdr); hdr.rewind(); chan.write(hdr); buf.position(Header.writtenSize()); chan.write(buf); chan.force(true); chan.close(); }
private static WAVData readFromStream(AudioInputStream aIn) throws UnsupportedAudioFileException, IOException { ReadableByteChannel aChannel = Channels.newChannel(aIn); AudioFormat fmt = aIn.getFormat(); int numChannels = fmt.getChannels(); int bits = fmt.getSampleSizeInBits(); int format = AL_FORMAT_MONO8; if ((bits == 8) && (numChannels == 1)) { format = AL_FORMAT_MONO8; } else if ((bits == 16) && (numChannels == 1)) { format = AL_FORMAT_MONO16; } else if ((bits == 8) && (numChannels == 2)) { format = AL_FORMAT_STEREO8; } else if ((bits == 16) && (numChannels == 2)) { format = AL_FORMAT_STEREO16; } int freq = Math.round(fmt.getSampleRate()); int size = aIn.available(); ByteBuffer buffer = ByteBuffer.allocateDirect(size); while (buffer.remaining() > 0) { aChannel.read(buffer); } buffer.rewind(); // Must byte swap on big endian platforms // Thanks to swpalmer on javagaming.org forums for hint at fix if ((bits == 16) && (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN)) { int len = buffer.remaining(); for (int i = 0; i < len; i += 2) { byte a = buffer.get(i); byte b = buffer.get(i + 1); buffer.put(i, b); buffer.put(i + 1, a); } } WAVData result = new WAVData(buffer, format, size, freq, false); aIn.close(); return result; }
protected synchronized Message receiveMessage() throws IOException { if (messageBuffer.size() > 0) { Message m = (Message) messageBuffer.get(0); messageBuffer.remove(0); return m; } try { InetSocketAddress remoteAddress = (InetSocketAddress) channel.receive(receiveBuffer); if (remoteAddress != null) { int len = receiveBuffer.position(); receiveBuffer.rewind(); receiveBuffer.get(buf, 0, len); try { IP address = IP.fromInetAddress(remoteAddress.getAddress()); int port = remoteAddress.getPort(); extractor.appendData(buf, 0, len, new SocketDescriptor(address, port)); receiveBuffer.clear(); extractor.updateAvailableMessages(); return extractor.nextMessage(); } catch (EOFException exc) { exc.printStackTrace(); System.err.println(buf.length + ", " + len); } catch (InvocationTargetException exc) { exc.printStackTrace(); } catch (IllegalAccessException exc) { exc.printStackTrace(); } catch (InstantiationException exc) { exc.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (InvalidCompressionMethodException e) { e.printStackTrace(); } } } catch (ClosedChannelException exc) { if (isKeepAlive()) { throw exc; } } return null; }
public static void main(String args[]) { int count; Path filepath = null; // First, obtain a path to the file. try { filepath = Paths.get("test.txt"); } catch (InvalidPathException e) { System.out.println("Path Error " + e); return; } // Next, obtain a channel to that file within a try-with-resources block. try (SeekableByteChannel fChan = Files.newByteChannel(filepath)) { // Allocate a buffer. ByteBuffer mBuf = ByteBuffer.allocate(128); do { // Read a buffer. count = fChan.read(mBuf); // Stop when end of file is reached. if (count != -1) { // Rewind the buffer so that it can be read. mBuf.rewind(); // Read bytes from the buffer and show // them on the screen as characters. for (int i = 0; i < count; i++) System.out.print((char) mBuf.get()); } } while (count != -1); System.out.println(); } catch (IOException e) { System.out.println("I/O Error " + e); } }
public static void main(String[] argv) { if (argv.length != 3) { usage(); } String tempFile = argv[0]; String testFile = argv[1]; int fileSize = Integer.valueOf(argv[2]).intValue(); int exitcode = 0; int numRead; int numThisBuf; int numWritten; if ((fileSize <= 0) || (fileSize % 4096 != 0)) { System.out.println("Error: size is not a multiple of 4096!!!!!!"); System.out.println(); usage(); } try { int bufSize = 4096; byte[] inBytes = new byte[bufSize]; byte[] outBytes = new byte[bufSize]; Random ioRandom = new Random(2006); ioRandom.nextBytes(outBytes); ByteBuffer inBuf = ByteBuffer.allocate(bufSize); ByteBuffer outBuf = ByteBuffer.wrap(outBytes); // // Loop forever // while (true) { // // Write the temporary file // FileOutputStream fos = new FileOutputStream(tempFile); FileChannel foc = fos.getChannel(); numWritten = 0; while (numWritten < fileSize) { outBuf.clear(); // sets limit to capacity & position to zero while (outBuf.hasRemaining()) { numWritten += foc.write(outBuf); } } // // Move to permanent location // FileChannel srcChannel = new FileInputStream(tempFile).getChannel(); FileChannel dstChannel = new FileOutputStream(testFile).getChannel(); dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); srcChannel.close(); dstChannel.close(); boolean success = (new File(tempFile)).delete(); if (!success) { System.out.println("Warning: unable to delete temporary file"); } // // Read and compare // FileInputStream fis = new FileInputStream(testFile); FileChannel fic = fis.getChannel(); for (numRead = 0, numThisBuf = 0; numRead < fileSize; numThisBuf = 0) { inBuf.rewind(); // Set the buffer position to 0 numThisBuf = fic.read(inBuf); while (numThisBuf < bufSize) { numThisBuf += fic.read(inBuf); } numRead += bufSize; inBuf.rewind(); // Set the buffer position to 0 inBuf.get(inBytes); boolean same = Arrays.equals(inBytes, outBytes); if (same = false) { System.out.println("Data read does not equal data written at " + numRead + " bytes"); } } } } catch (FileNotFoundException fnfe) { fnfe.printStackTrace(System.err); exitcode = 1; // break; } catch (SecurityException se) { se.printStackTrace(System.err); exitcode = 1; // break; } catch (Throwable t) { t.printStackTrace(System.err); exitcode = 1; } System.exit(exitcode); }