use of java.nio.channels.WritableByteChannel in project robovm by robovm.
the class ChannelsTest method testNewChannelOutputStream_inputNull.
// test if fout to change is null
public void testNewChannelOutputStream_inputNull() throws IOException {
int writeres = this.testNum;
ByteBuffer writebuf = ByteBuffer.allocate(this.writebufSize);
for (int val = 0; val < this.writebufSize / 2; val++) {
writebuf.putChar((char) (val + 64));
}
this.fouts = null;
try {
WritableByteChannel rbChannel = Channels.newChannel(this.fouts);
writeres = rbChannel.write(writebuf);
assertEquals(0, writeres);
writebuf.flip();
writeres = rbChannel.write(writebuf);
fail("Should throw NPE.");
} catch (NullPointerException expected) {
}
}
use of java.nio.channels.WritableByteChannel in project robovm by robovm.
the class ChannelsTest method testNewChannelOutputStream_BufNull.
// test if write buf is null
public void testNewChannelOutputStream_BufNull() throws IOException {
int writeres = this.testNum;
ByteBuffer writebuf = null;
try {
this.fouts = new FileOutputStream(tmpFile);
} catch (FileNotFoundException e) {
fail();
}
WritableByteChannel rbChannel = Channels.newChannel(this.fouts);
try {
writeres = rbChannel.write(writebuf);
fail();
} catch (NullPointerException e) {
// correct
}
assertEquals(this.testNum, writeres);
}
use of java.nio.channels.WritableByteChannel in project robovm by robovm.
the class ChannelsTest method testnewWriterCharsetError.
public void testnewWriterCharsetError() throws Exception {
this.fouts = new FileOutputStream(tmpFile);
WritableByteChannel wbChannel = Channels.newChannel(this.fouts);
try {
Channels.newWriter(wbChannel, Charset.forName(BAD_CODE_SET).newEncoder(), -1);
fail();
} catch (UnsupportedCharsetException e) {
// correct
}
}
use of java.nio.channels.WritableByteChannel in project beat-link by brunchboy.
the class MetadataFinder method copyTracksToCache.
/**
* Finish the process of copying a list of tracks to a metadata cache, once they have been listed. This code
* is shared between the implementations that work with the full track list and with playlists.
*
* @param trackListEntries the list of menu items identifying which tracks need to be copied to the metadata
* cache
* @param client the connection to the dbserver on the player whose metadata is being cached
* @param slot the slot in which the media to be cached can be found
* @param cache the file into which the metadata cache should be written
* @param listener will be informed after each track is added to the cache file being created and offered
* the opportunity to cancel the process
*
* @throws IOException if there is a problem communicating with the player or writing the cache file.
*/
private static void copyTracksToCache(List<Message> trackListEntries, Client client, SlotReference slot, File cache, MetadataCreationUpdateListener listener) throws IOException {
FileOutputStream fos = null;
BufferedOutputStream bos = null;
ZipOutputStream zos = null;
WritableByteChannel channel = null;
final Set<Integer> artworkAdded = new HashSet<Integer>();
try {
fos = new FileOutputStream(cache);
bos = new BufferedOutputStream(fos);
zos = new ZipOutputStream(bos);
zos.setMethod(ZipOutputStream.DEFLATED);
// Add a marker so we can recognize this as a metadata archive. I would use the ZipFile comment, but
// that is not available until Java 7, and Beat Link is supposed to be backwards compatible with Java 6.
ZipEntry zipEntry = new ZipEntry(CACHE_FORMAT_ENTRY);
zos.putNextEntry(zipEntry);
zos.write(CACHE_FORMAT_IDENTIFIER.getBytes("UTF-8"));
// Write the actual metadata entries
channel = Channels.newChannel(zos);
final int totalToCopy = trackListEntries.size();
int tracksCopied = 0;
for (Message entry : trackListEntries) {
if (entry.getMenuItemType() != Message.MenuItemType.TRACK_LIST_ENTRY) {
throw new IOException("Received unexpected item type. Needed TRACK_LIST_ENTRY, got: " + entry);
}
int rekordboxId = (int) ((NumberField) entry.arguments.get(1)).getValue();
TrackMetadata track = queryMetadata(new TrackReference(slot, rekordboxId), client);
if (track != null) {
logger.debug("Adding metadata with ID {}", rekordboxId);
zipEntry = new ZipEntry(getMetadataEntryName(rekordboxId));
zos.putNextEntry(zipEntry);
for (Message metadataItem : track.rawItems) {
metadataItem.write(channel);
}
// So we know to stop reading
MENU_FOOTER_MESSAGE.write(channel);
} else {
logger.warn("Unable to retrieve metadata with ID {}", rekordboxId);
}
// Now the album art, if any
if (track != null && track.getArtworkId() != 0 && !artworkAdded.contains(track.getArtworkId())) {
logger.debug("Adding artwork with ID {}", track.getArtworkId());
zipEntry = new ZipEntry(getArtworkEntryName(track.getArtworkId()));
zos.putNextEntry(zipEntry);
Util.writeFully(getArtwork(track.getArtworkId(), slot.slot, client), channel);
artworkAdded.add(track.getArtworkId());
}
BeatGrid beatGrid = getBeatGrid(rekordboxId, slot.slot, client);
if (beatGrid != null) {
logger.debug("Adding beat grid with ID {}", rekordboxId);
zipEntry = new ZipEntry(getBeatGridEntryName(rekordboxId));
zos.putNextEntry(zipEntry);
Util.writeFully(beatGrid.getRawData(), channel);
}
CueList cueList = getCueList(rekordboxId, slot.slot, client);
if (cueList != null) {
logger.debug("Adding cue list entry with ID {}", rekordboxId);
zipEntry = new ZipEntry((getCueListEntryName(rekordboxId)));
zos.putNextEntry(zipEntry);
cueList.rawMessage.write(channel);
}
WaveformPreview preview = getWaveformPreview(rekordboxId, slot.slot, client);
if (preview != null) {
logger.debug("Adding waveform preview entry with ID {}", rekordboxId);
zipEntry = new ZipEntry((getWaveformPreviewEntryName(rekordboxId)));
zos.putNextEntry(zipEntry);
preview.rawMessage.write(channel);
}
// TODO: Include waveforms (once supported), etc.
if (listener != null) {
if (!listener.cacheUpdateContinuing(track, ++tracksCopied, totalToCopy)) {
logger.info("Track metadata cache creation canceled by listener");
if (!cache.delete()) {
logger.warn("Unable to delete cache metadata file, {}", cache);
}
return;
}
}
}
} finally {
try {
if (channel != null) {
channel.close();
}
} catch (Exception e) {
logger.error("Problem closing byte channel for writing to metadata cache", e);
}
try {
if (zos != null) {
zos.close();
}
} catch (Exception e) {
logger.error("Problem closing Zip Output Stream of metadata cache", e);
}
try {
if (bos != null) {
bos.close();
}
} catch (Exception e) {
logger.error("Problem closing Buffered Output Stream of metadata cache", e);
}
try {
if (fos != null) {
fos.close();
}
} catch (Exception e) {
logger.error("Problem closing File Output Stream of metadata cache", e);
}
}
}
use of java.nio.channels.WritableByteChannel in project jdk8u_jdk by JetBrains.
the class TransferToChannel method transferFileToUserChannel.
static void transferFileToUserChannel() throws Exception {
long remainingBytes = in.size();
long size = remainingBytes;
WritableByteChannel wbc = new WritableByteChannel() {
Random rand = new Random(0);
public int write(ByteBuffer src) throws IOException {
int read = src.remaining();
byte[] incoming = new byte[read];
src.get(incoming);
checkData(incoming, read);
return read == 0 ? -1 : read;
}
public boolean isOpen() {
return true;
}
public void close() throws IOException {
}
void checkData(byte[] incoming, int size) {
byte[] expected = new byte[size];
rand.nextBytes(expected);
for (int i = 0; i < size; i++) if (incoming[i] != expected[i])
throw new RuntimeException("Data corrupted");
}
};
while (remainingBytes > 0) {
long bytesTransferred = in.transferTo(size - remainingBytes, Math.min(CHUNK_SIZE, remainingBytes), wbc);
if (bytesTransferred >= 0)
remainingBytes -= bytesTransferred;
else
throw new Exception("transfer failed");
}
}
Aggregations