Skip to content
This repository has been archived by the owner on Feb 8, 2019. It is now read-only.

Commit

Permalink
rename package
Browse files Browse the repository at this point in the history
  • Loading branch information
gwhalin committed Mar 17, 2008
1 parent 83f30e0 commit 552e05c
Show file tree
Hide file tree
Showing 13 changed files with 5,921 additions and 0 deletions.
137 changes: 137 additions & 0 deletions src/com/whalin/MemCached/ByteBufArrayInputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/**
* Copyright (c) 2007 Greg Whalin
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the BSD license
*
* This library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE.
*
* You should have received a copy of the BSD License along with this
* library.
*
* @author greg whalin <[email protected]>
* @version 2.0
*/
package com.danga.MemCached;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

public final class ByteBufArrayInputStream extends InputStream implements LineInputStream {
private ByteBuffer[] bufs;
private int currentBuf = 0;

public ByteBufArrayInputStream( List<ByteBuffer> bufs ) throws Exception {
this( bufs.toArray( new ByteBuffer[] {} ) );
}

public ByteBufArrayInputStream( ByteBuffer[] bufs ) throws Exception {
if ( bufs == null || bufs.length == 0 )
throw new Exception( "buffer is empty" );

this.bufs = bufs;
for ( ByteBuffer b : bufs )
b.flip();
}

public int read() {
do {
if ( bufs[currentBuf].hasRemaining() )
return bufs[currentBuf].get();
currentBuf++;
}
while ( currentBuf < bufs.length );

currentBuf--;
return -1;
}

public int read( byte[] buf ) {
int len = buf.length;
int bufPos = 0;
do {
if ( bufs[currentBuf].hasRemaining() ) {
int n = Math.min( bufs[currentBuf].remaining(), len-bufPos );
bufs[currentBuf].get( buf, bufPos, n );
bufPos += n;
}
currentBuf++;
}
while ( currentBuf < bufs.length && bufPos < len );

currentBuf--;

if ( bufPos > 0 || ( bufPos == 0 && len == 0 ) )
return bufPos;
else
return -1;
}

public String readLine() throws IOException {
byte[] b = new byte[1];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
boolean eol = false;

while ( read( b, 0, 1 ) != -1 ) {
if ( b[0] == 13 ) {
eol = true;
}
else {
if ( eol ) {
if ( b[0] == 10 )
break;
eol = false;
}
}

// cast byte into char array
bos.write( b, 0, 1 );
}

if ( bos == null || bos.size() <= 0 ) {
throw new IOException( "++++ Stream appears to be dead, so closing it down" );
}

// else return the string
return bos.toString().trim();
}

public void clearEOL() throws IOException {
byte[] b = new byte[1];
boolean eol = false;
while ( read( b, 0, 1 ) != -1 ) {

// only stop when we see
// \r (13) followed by \n (10)
if ( b[0] == 13 ) {
eol = true;
continue;
}

if ( eol ) {
if ( b[0] == 10 )
break;
eol = false;
}
}
}

public String toString() {
StringBuilder sb = new StringBuilder( "ByteBufArrayIS: " );
sb.append( bufs.length ).append( " bufs of sizes: \n" );

for ( int i=0; i < bufs.length; i++ ) {
sb.append( " " )
.append (i ).append( ": " ).append( bufs[i] ).append( "\n" );
}
return sb.toString();
}
}
46 changes: 46 additions & 0 deletions src/com/whalin/MemCached/ContextObjectInputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* MemCached Java client
* Copyright (c) 2008 Greg Whalin
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the BSD license
*
* This library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE.
*
* You should have received a copy of the BSD License along with this
* library.
*
* Adds the ability for the MemCached client to be initialized
* with a custom class loader. This will allow for the
* deserialization of classes that are not visible to the system
* class loader.
*
* @author Vin Chawla <[email protected]>
* @version 2.0
*/
package com.danga.MemCached;

import java.util.*;
import java.util.zip.*;
import java.io.*;

public class ContextObjectInputStream extends ObjectInputStream {

ClassLoader mLoader;

public ContextObjectInputStream( InputStream in, ClassLoader loader ) throws IOException, SecurityException {
super( in );
mLoader = loader;
}

protected Class resolveClass( ObjectStreamClass v ) throws IOException, ClassNotFoundException {
if ( mLoader == null )
return super.resolveClass( v );
else
return Class.forName( v.getName(), true, mLoader );
}
}
74 changes: 74 additions & 0 deletions src/com/whalin/MemCached/ErrorHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* MemCached Java client
* Copyright (c) 2007 Greg Whalin
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the BSD license
*
* This library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE.
*
* You should have received a copy of the BSD License along with this
* library.
*
* This is an interface implemented by classes that want to receive callbacks
* in the event of an error in {@link MemCachedClient}. The implementor can do
* things like flush caches or perform additioonal logging.
*
* @author Dan Zivkovic <[email protected]>
* @version 2.0
*/
package com.danga.MemCached;

public interface ErrorHandler {

/**
* Called for errors thrown during initialization.
*/
public void handleErrorOnInit( final MemCachedClient client ,
final Throwable error );

/**
* Called for errors thrown during {@link MemCachedClient#get(String)} and related methods.
*/
public void handleErrorOnGet( final MemCachedClient client ,
final Throwable error ,
final String cacheKey );

/**
* Called for errors thrown during {@link MemCachedClient#getMulti(String)} and related methods.
*/
public void handleErrorOnGet( final MemCachedClient client ,
final Throwable error ,
final String[] cacheKeys );

/**
* Called for errors thrown during {@link MemCachedClient#set(String,Object)} and related methods.
*/
public void handleErrorOnSet( final MemCachedClient client ,
final Throwable error ,
final String cacheKey );

/**
* Called for errors thrown during {@link MemCachedClient#delete(String)} and related methods.
*/
public void handleErrorOnDelete( final MemCachedClient client ,
final Throwable error ,
final String cacheKey );

/**
* Called for errors thrown during {@link MemCachedClient#flushAll()} and related methods.
*/
public void handleErrorOnFlush( final MemCachedClient client ,
final Throwable error );

/**
* Called for errors thrown during {@link MemCachedClient#stats()} and related methods.
*/
public void handleErrorOnStats( final MemCachedClient client ,
final Throwable error );

} // interface
45 changes: 45 additions & 0 deletions src/com/whalin/MemCached/LineInputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* MemCached Java client
* Copyright (c) 2007 Greg Whalin
* All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the BSD license
*
* This library is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE.
*
* You should have received a copy of the BSD License along with this
* library.
*
* @author greg whalin <[email protected]>
* @version 2.0
*/
package com.danga.MemCached;

import java.io.IOException;

public interface LineInputStream {

/**
* Read everything up to the next end-of-line. Does
* not include the end of line, though it is consumed
* from the input.
* @return All next up to the next end of line.
*/
public String readLine() throws IOException;

/**
* Read everything up to and including the end of line.
*/
public void clearEOL() throws IOException;

/**
* Read some bytes.
* @param buf The buffer into which read.
* @return The number of bytes actually read, or -1 if none could be read.
*/
public int read( byte[] buf ) throws IOException;
}
Loading

0 comments on commit 552e05c

Please sign in to comment.