Skip to content

Commit

Permalink
Change some minor stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
fxb committed Apr 26, 2010
1 parent 420a029 commit c87e90b
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 24 deletions.
40 changes: 35 additions & 5 deletions src/de/felixbruns/jotify/JotifyConnection.java
Expand Up @@ -122,7 +122,7 @@ public void login(String username, String password) throws ConnectionException,
this.protocol.addListener(this);

/* Start I/O thread. */
new Thread(this).start();
new Thread(this, "I/O-Thread").start();
}

/**
Expand Down Expand Up @@ -494,13 +494,43 @@ public List<Track> browseTracks(List<String> ids) throws TimeoutException {
byte[] data;

/* Create cache hash. */
String hash = "";
StringBuffer hashBuffer = new StringBuffer();

for(String id : ids){
hash += id;
for(int i = 0; i < ids.size(); i++){
String id = ids.get(i);

/*
* Check if id is a 32-character hex string,
* if not try to parse it as a Spotify URI.
*/
if(id.length() != 32 && !Hex.isHex(id)){
try{
Link link = Link.create(id);

if(!link.isTrackLink()){
throw new IllegalArgumentException(
"Browse type doesn't match given Spotify URI."
);
}

id = link.getId();

/* Set parsed id in list. */
ids.set(i, id);
}
catch(InvalidSpotifyURIException e){
throw new IllegalArgumentException(
"Given id is neither a 32-character " +
"hex string nor a valid Spotify URI."
);
}
}

/* Append id to buffer in order to create a cache hash. */
hashBuffer.append(id);
}

hash = Hex.toHex(Hash.sha1(Hex.toBytes(hash)));
String hash = Hex.toHex(Hash.sha1(Hex.toBytes(hashBuffer.toString())));

/* Check cache. */
if(this.cache != null && this.cache.contains("browse", hash)){
Expand Down
13 changes: 9 additions & 4 deletions src/de/felixbruns/jotify/media/Link.java
Expand Up @@ -136,17 +136,22 @@ else if(type.equals("track")){
throw new InvalidSpotifyURIException();
}

this.id = Link.toHex(mediaMatcher.group(2));
this.id = Link.toHex(mediaMatcher.group(2));
this.user = null;
this.query = null;
}
/* Check if URI matches playlist pattern. */
else if(playlistMatcher.matches()){
this.type = Type.PLAYLIST;
this.user = playlistMatcher.group(1);
this.id = Link.toHex(playlistMatcher.group(2));
this.type = Type.PLAYLIST;
this.user = playlistMatcher.group(1);
this.id = Link.toHex(playlistMatcher.group(2));
this.query = null;
}
/* Check if URI matches search pattern. */
else if(searchMatcher.matches()){
this.type = Type.SEARCH;
this.id = null;
this.user = null;

try{
this.query = URLDecoder.decode(searchMatcher.group(1), "UTF-8");
Expand Down
1 change: 0 additions & 1 deletion src/de/felixbruns/jotify/media/parser/XMLUserParser.java
Expand Up @@ -75,7 +75,6 @@ private void parseProduct(User user) throws XMLStreamException, XMLParserExcepti
while(this.reader.next() == START_ELEMENT){
user.setProperty(this.reader.getLocalName(), this.getElementString());
}

}

/**
Expand Down
50 changes: 36 additions & 14 deletions src/de/felixbruns/jotify/player/SpotifyInputStream.java
Expand Up @@ -343,7 +343,7 @@ public int read() throws IOException {
/* Acquire request lock. */
this.requestLock.lock();

/* Request data until threshold (TODO) is reached. */
/* Request data. */
while(this.available() == 0){
/* Calculate stream offset for next data request. */
this.streamOffset = this.readIndex * CHUNK_SIZE;
Expand All @@ -353,7 +353,7 @@ public int read() throws IOException {
break;
}

/* Wait until a chunk arrived. TODO: timeout here, throw IOException... */
/* Wait until a chunk arrived. (TODO: Timeout, then throw IOException!?) */
this.requestCondition.awaitUninterruptibly();
}

Expand Down Expand Up @@ -419,6 +419,11 @@ public int read() throws IOException {
* negative, or {@code len} is greater than {@code b.length - off}
*/
public int read(byte[] b, int off, int len) throws IOException {
/* Check buffer. */
if(b == null){
throw new NullPointerException();
}

/* Check offset and length arguments. */
if(off < 0 || len < 0 || len > b.length - off){
throw new IndexOutOfBoundsException();
Expand All @@ -435,22 +440,17 @@ public int read(byte[] b, int off, int len) throws IOException {
/* Acquire request lock. */
this.requestLock.lock();

/* Request data until threshold (TODO) is reached. */
/* Request data. */
while(this.available() == 0){
/* Calculate stream offset for next data request. TODO: search next hole. */
//if(this.readPosition > 0){
// this.streamOffset = (this.readIndex + 1) * CHUNK_SIZE;
//}
//else{
this.streamOffset = this.readIndex * CHUNK_SIZE;
//}
/* Calculate stream offset for next data request. (TODO: Search next hole in data) */
this.streamOffset = this.readIndex * CHUNK_SIZE;

/* Try to request data, if this fails exit loop. */
if(!this.requestData()){
break;
}

/* Wait until a chunk arrived. */
/* Wait until a chunk arrived. (TODO: Timeout, then throw IOException!?) */
this.requestCondition.awaitUninterruptibly();
}

Expand Down Expand Up @@ -497,14 +497,36 @@ public int read(byte[] b, int off, int len) throws IOException {
}
}

//this.debug();

/* Return number of actually read bytes. */
return read;
}

/**
* TODO
* Reads some number of bytes from the input stream and stores them into
* the buffer array {@code b}. The number of bytes actually read is returned
* as an integer. This method blocks until input data is available, end of
* stream is detected, or an exception is thrown.
*
* If the length of {@code b} is zero, then no bytes are read and 0 is returned;
* otherwise, there is an attempt to read at least one byte. If no byte is available
* because the stream is at the end, the value -1 is returned; otherwise, at least
* one byte is read and stored into {@code b}.
*
* The read(b) method for class InputStream has the same effect as:
*
* {@code read(b, 0, b.length)}
*
* @param b The buffer into which the data is read.
*
* @return The total number of bytes read into the buffer, or -1 is there is no more
* data because the end of the stream has been reached.
*
* @throws IOException If the first byte cannot be read for any reason other than the
* end of the stream, if the input stream has been closed, or if some other
* I/O error occurs.
* @throws NullPointerException If {@code b} is null.
*
* @see {@link #read(byte[], int, int)}
*/
public int read(byte[] b) throws IOException {
return this.read(b, 0, b.length);
Expand Down

0 comments on commit c87e90b

Please sign in to comment.