Created
October 4, 2012 06:18
-
-
Save dsdstudio/3831756 to your computer and use it in GitHub Desktop.
LargeDataChunkDecoder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.dsdstudio.sharebox.server.socket; | |
import org.jboss.netty.buffer.ChannelBuffer; | |
import org.jboss.netty.channel.Channel; | |
import org.jboss.netty.channel.ChannelHandlerContext; | |
import org.jboss.netty.handler.codec.replay.ReplayingDecoder; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
/** | |
* DataChunkDecoder | |
* User: bhkim | |
* Date: 12. 9. 23. | |
* Time: 오후 9:05 | |
*/ | |
public class LargeDataChunkDecoder extends ReplayingDecoder<LargeDataChunkDecoder.State> { | |
private final Logger logger = LoggerFactory.getLogger(LargeDataChunkDecoder.class); | |
private int filenamelen; | |
/** | |
* Buffer State | |
* fnlen|fnamedata|flength|fdata | |
*/ | |
protected enum State { | |
READ_FILENAME_LENGTH, | |
READ_FILENAME_BUFFER, | |
READ_FILEDATA_LENGTH, | |
READ_FILEDATA_BUFFER, | |
ENDOFFILE | |
} | |
public LargeDataChunkDecoder() { | |
super(State.READ_FILENAME_LENGTH); | |
} | |
@Override | |
protected Object decode(ChannelHandlerContext ctx, Channel channel, ChannelBuffer buffer, State state) throws Exception { | |
logger.debug(state + " "); | |
switch (state) { | |
case READ_FILENAME_LENGTH: | |
this.filenamelen = buffer.readInt(); | |
logger.debug("filenamelen : {}", filenamelen); | |
this.checkpoint(State.READ_FILENAME_BUFFER); | |
return null; | |
case READ_FILENAME_BUFFER: | |
byte[] b = new byte[this.filenamelen]; | |
buffer.readBytes(b); | |
String filename = new String(b); | |
logger.debug("filename : {}", filename); | |
this.checkpoint(State.READ_FILEDATA_LENGTH); | |
return filename; | |
case READ_FILEDATA_LENGTH: | |
long filedatalen = buffer.readLong(); | |
logger.debug("datalen : {}", filedatalen); | |
this.checkpoint(State.READ_FILEDATA_BUFFER); | |
return filedatalen; | |
case READ_FILEDATA_BUFFER: | |
int readLimit = actualReadableBytes(); | |
logger.debug("chunksize : {}", readLimit); | |
if (readLimit == 0) { | |
channel.close(); | |
this.checkpoint(State.ENDOFFILE); | |
return null; | |
} | |
// chunk size만큼 byte array 할당 | |
byte[] fdata = new byte[readLimit]; | |
buffer.readBytes(fdata); | |
return fdata; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment