Java Code Examples for org.apache.tomcat.util.buf.ByteChunk#getBuffer()
The following examples show how to use
org.apache.tomcat.util.buf.ByteChunk#getBuffer() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Http11OutputBuffer.java From Tomcat8-Source-Read with MIT License | 6 votes |
/** * This method will write the contents of the specified message bytes * buffer to the output stream, without filtering. This method is meant to * be used to write the response header. * * @param mb data to be written */ private void write(MessageBytes mb) { if (mb.getType() != MessageBytes.T_BYTES) { mb.toBytes(); ByteChunk bc = mb.getByteChunk(); // Need to filter out CTLs excluding TAB. ISO-8859-1 and UTF-8 // values will be OK. Strings using other encodings may be // corrupted. byte[] buffer = bc.getBuffer(); for (int i = bc.getOffset(); i < bc.getLength(); i++) { // byte values are signed i.e. -128 to 127 // The values are used unsigned. 0 to 31 are CTLs so they are // filtered (apart from TAB which is 9). 127 is a control (DEL). // The values 128 to 255 are all OK. Converting those to signed // gives -128 to -1. if ((buffer[i] > -1 && buffer[i] <= 31 && buffer[i] != 9) || buffer[i] == 127) { buffer[i] = ' '; } } } write(mb.getByteChunk()); }
Example 2
Source File: Cookies.java From tomcatsrc with Apache License 2.0 | 6 votes |
/** * Unescapes any double quotes in the given cookie value. * * @param bc The cookie value to modify */ private static void unescapeDoubleQuotes(ByteChunk bc) { if (bc == null || bc.getLength() == 0 || bc.indexOf('"', 0) == -1) { return; } int src = bc.getStart(); int end = bc.getEnd(); int dest = src; byte[] buffer = bc.getBuffer(); while (src < end) { if (buffer[src] == '\\' && src < end && buffer[src+1] == '"') { src++; } buffer[dest] = buffer[src]; dest ++; src ++; } bc.setEnd(dest); }
Example 3
Source File: CoyoteAdapter.java From tomcatsrc with Apache License 2.0 | 6 votes |
/** * Character conversion of the a US-ASCII MessageBytes. */ protected void convertMB(MessageBytes mb) { // This is of course only meaningful for bytes if (mb.getType() != MessageBytes.T_BYTES) { return; } ByteChunk bc = mb.getByteChunk(); CharChunk cc = mb.getCharChunk(); int length = bc.getLength(); cc.allocate(length, -1); // Default encoding: fast conversion byte[] bbuf = bc.getBuffer(); char[] cbuf = cc.getBuffer(); int start = bc.getStart(); for (int i = 0; i < length; i++) { cbuf[i] = (char) (bbuf[i + start] & 0xff); } mb.setChars(cbuf, 0, length); }
Example 4
Source File: CoyoteAdapter.java From Tomcat7.0.67 with Apache License 2.0 | 6 votes |
/** * Character conversion of the a US-ASCII MessageBytes. */ protected void convertMB(MessageBytes mb) { // This is of course only meaningful for bytes if (mb.getType() != MessageBytes.T_BYTES) { return; } ByteChunk bc = mb.getByteChunk(); CharChunk cc = mb.getCharChunk(); int length = bc.getLength(); cc.allocate(length, -1); // Default encoding: fast conversion byte[] bbuf = bc.getBuffer(); char[] cbuf = cc.getBuffer(); int start = bc.getStart(); for (int i = 0; i < length; i++) { cbuf[i] = (char) (bbuf[i + start] & 0xff); } mb.setChars(cbuf, 0, length); }
Example 5
Source File: Cookies.java From Tomcat7.0.67 with Apache License 2.0 | 6 votes |
/** * Unescapes any double quotes in the given cookie value. * * @param bc The cookie value to modify */ private static void unescapeDoubleQuotes(ByteChunk bc) { if (bc == null || bc.getLength() == 0 || bc.indexOf('"', 0) == -1) { return; } int src = bc.getStart(); int end = bc.getEnd(); int dest = src; byte[] buffer = bc.getBuffer(); while (src < end) { if (buffer[src] == '\\' && src < end && buffer[src+1] == '"') { src++; } buffer[dest] = buffer[src]; dest ++; src ++; } bc.setEnd(dest); }
Example 6
Source File: InternalAprOutputBuffer.java From Tomcat7.0.67 with Apache License 2.0 | 6 votes |
/** * Write chunk. */ @Override public int doWrite(ByteChunk chunk, Response res) throws IOException { int len = chunk.getLength(); int start = chunk.getStart(); byte[] b = chunk.getBuffer(); while (len > 0) { int thisTime = len; if (bbuf.position() == bbuf.capacity()) { flushBuffer(); } if (thisTime > bbuf.capacity() - bbuf.position()) { thisTime = bbuf.capacity() - bbuf.position(); } bbuf.put(b, start, thisTime); len = len - thisTime; start = start + thisTime; } byteCount += chunk.getLength(); return chunk.getLength(); }
Example 7
Source File: CoyoteAdapter.java From Tomcat8-Source-Read with MIT License | 6 votes |
/** * Character conversion of the a US-ASCII MessageBytes. * * @param mb The MessageBytes instance containing the bytes that should be converted to chars */ protected void convertMB(MessageBytes mb) { // This is of course only meaningful for bytes if (mb.getType() != MessageBytes.T_BYTES) { return; } ByteChunk bc = mb.getByteChunk(); CharChunk cc = mb.getCharChunk(); int length = bc.getLength(); cc.allocate(length, -1); // Default encoding: fast conversion byte[] bbuf = bc.getBuffer(); char[] cbuf = cc.getBuffer(); int start = bc.getStart(); for (int i = 0; i < length; i++) { cbuf[i] = (char) (bbuf[i + start] & 0xff); } mb.setChars(cbuf, 0, length); }
Example 8
Source File: InternalNioOutputBuffer.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
/** * Write chunk. */ @Override public int doWrite(ByteChunk chunk, Response res) throws IOException { int len = chunk.getLength(); int start = chunk.getStart(); byte[] b = chunk.getBuffer(); addToBB(b, start, len); byteCount += chunk.getLength(); return chunk.getLength(); }
Example 9
Source File: InternalAprOutputBuffer.java From tomcatsrc with Apache License 2.0 | 5 votes |
/** * Write chunk. */ @Override public int doWrite(ByteChunk chunk, Response res) throws IOException { try { int len = chunk.getLength(); int start = chunk.getStart(); byte[] b = chunk.getBuffer(); while (len > 0) { int thisTime = len; if (bbuf.position() == bbuf.capacity()) { flushBuffer(); } if (thisTime > bbuf.capacity() - bbuf.position()) { thisTime = bbuf.capacity() - bbuf.position(); } bbuf.put(b, start, thisTime); len = len - thisTime; start = start + thisTime; } byteCount += chunk.getLength(); return chunk.getLength(); } catch (IOException ioe) { response.action(ActionCode.CLOSE_NOW, ioe); // Re-throw throw ioe; } }
Example 10
Source File: AbstractHttp11Processor.java From tomcatsrc with Apache License 2.0 | 5 votes |
/** * Specialized utility method: find a sequence of lower case bytes inside * a ByteChunk. */ protected int findBytes(ByteChunk bc, byte[] b) { byte first = b[0]; byte[] buff = bc.getBuffer(); int start = bc.getStart(); int end = bc.getEnd(); // Look for first char int srcEnd = b.length; for (int i = start; i <= (end - srcEnd); i++) { if (Ascii.toLower(buff[i]) != first) { continue; } // found first char, now look for a match int myPos = i+1; for (int srcPos = 1; srcPos < srcEnd;) { if (Ascii.toLower(buff[myPos++]) != b[srcPos++]) { break; } if (srcPos == srcEnd) { return i - start; // found it } } } return -1; }
Example 11
Source File: AbstractHttp11Processor.java From Tomcat7.0.67 with Apache License 2.0 | 5 votes |
/** * Specialized utility method: find a sequence of lower case bytes inside * a ByteChunk. */ protected int findBytes(ByteChunk bc, byte[] b) { byte first = b[0]; byte[] buff = bc.getBuffer(); int start = bc.getStart(); int end = bc.getEnd(); // Look for first char int srcEnd = b.length; for (int i = start; i <= (end - srcEnd); i++) { if (Ascii.toLower(buff[i]) != first) { continue; } // found first char, now look for a match int myPos = i+1; for (int srcPos = 1; srcPos < srcEnd;) { if (Ascii.toLower(buff[myPos++]) != b[srcPos++]) { break; } if (srcPos == srcEnd) { return i - start; // found it } } } return -1; }
Example 12
Source File: LegacyCookieProcessor.java From Tomcat8-Source-Read with MIT License | 5 votes |
/** * Unescapes any double quotes in the given cookie value. * * @param bc The cookie value to modify */ private static final void unescapeDoubleQuotes(ByteChunk bc) { if (bc == null || bc.getLength() == 0 || bc.indexOf('"', 0) == -1) { return; } // Take a copy of the buffer so the original cookie header is not // modified by this unescaping. byte[] original = bc.getBuffer(); int len = bc.getLength(); byte[] copy = new byte[len]; System.arraycopy(original, bc.getStart(), copy, 0, len); int src = 0; int dest = 0; while (src < len) { if (copy[src] == '\\' && src < len && copy[src+1] == '"') { src++; } copy[dest] = copy[src]; dest ++; src ++; } bc.setBytes(copy, 0, dest); }
Example 13
Source File: Http11OutputBuffer.java From Tomcat8-Source-Read with MIT License | 5 votes |
/** * Write chunk. * * @deprecated Unused. Will be removed in Tomcat 9. Use * {@link #doWrite(ByteBuffer)} */ @Deprecated @Override public int doWrite(ByteChunk chunk) throws IOException { int len = chunk.getLength(); int start = chunk.getStart(); byte[] b = chunk.getBuffer(); socketWrapper.write(isBlocking(), b, start, len); byteCount += len; return len; }
Example 14
Source File: AjpMessage.java From Tomcat8-Source-Read with MIT License | 5 votes |
/** * Write a MessageBytes out at the current write position. A null * MessageBytes is encoded as a string with length 0. * * @param mb The data to write */ public void appendBytes(MessageBytes mb) { if (mb == null) { log.error(sm.getString("ajpmessage.null"), new NullPointerException()); appendInt(0); appendByte(0); return; } if (mb.getType() != MessageBytes.T_BYTES) { mb.toBytes(); ByteChunk bc = mb.getByteChunk(); // Need to filter out CTLs excluding TAB. ISO-8859-1 and UTF-8 // values will be OK. Strings using other encodings may be // corrupted. byte[] buffer = bc.getBuffer(); for (int i = bc.getOffset(); i < bc.getLength(); i++) { // byte values are signed i.e. -128 to 127 // The values are used unsigned. 0 to 31 are CTLs so they are // filtered (apart from TAB which is 9). 127 is a control (DEL). // The values 128 to 255 are all OK. Converting those to signed // gives -128 to -1. if ((buffer[i] > -1 && buffer[i] <= 31 && buffer[i] != 9) || buffer[i] == 127) { buffer[i] = ' '; } } } appendByteChunk(mb.getByteChunk()); }
Example 15
Source File: Base64.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
/** * Decodes Base64 data into octets * * @param base64DataBC Byte array containing Base64 data * @param decodedDataBC The decoded data bytes */ public static void decode( ByteChunk base64DataBC, ByteChunk decodedDataBC) { int start = base64DataBC.getStart(); int end = base64DataBC.getEnd(); byte[] base64Data = base64DataBC.getBuffer(); decodedDataBC.recycle(); // handle the edge case, so we don't have to worry about it later if(end - start == 0) { return; } int numberQuadruple = (end - start)/FOURBYTE; byte b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0; // Throw away anything not in base64Data int encodedIndex = 0; int dataIndex = start; byte[] decodedData = null; { // this sizes the output array properly - rlw int lastData = end - start; // ignore the '=' padding while (base64Data[start+lastData-1] == PAD) { if (--lastData == 0) { return; } } decodedDataBC.allocate(lastData - numberQuadruple, -1); decodedDataBC.setEnd(lastData - numberQuadruple); decodedData = decodedDataBC.getBuffer(); } for (int i = 0; i < numberQuadruple; i++) { dataIndex = start + i * 4; marker0 = base64Data[dataIndex + 2]; marker1 = base64Data[dataIndex + 3]; b1 = base64Alphabet[base64Data[dataIndex]]; b2 = base64Alphabet[base64Data[dataIndex +1]]; if (marker0 != PAD && marker1 != PAD) { //No PAD e.g 3cQl b3 = base64Alphabet[ marker0 ]; b4 = base64Alphabet[ marker1 ]; decodedData[encodedIndex] = (byte) (( b1 <<2 | b2>>4 ) & 0xff); decodedData[encodedIndex + 1] = (byte) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff); decodedData[encodedIndex + 2] = (byte) (( b3<<6 | b4 ) & 0xff); } else if (marker0 == PAD) { //Two PAD e.g. 3c[Pad][Pad] decodedData[encodedIndex] = (byte) (( b1 <<2 | b2>>4 ) & 0xff); } else if (marker1 == PAD) { //One PAD e.g. 3cQ[Pad] b3 = base64Alphabet[ marker0 ]; decodedData[encodedIndex] = (byte) (( b1 <<2 | b2>>4 ) & 0xff); decodedData[encodedIndex + 1] = (byte) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff); } encodedIndex += 3; } }
Example 16
Source File: Base64.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
/** * Decodes Base64 data into octets * * @param base64DataBC Byte array containing Base64 data * @param decodedDataCC The decoded data chars */ public static void decode( ByteChunk base64DataBC, CharChunk decodedDataCC) { int start = base64DataBC.getStart(); int end = base64DataBC.getEnd(); byte[] base64Data = base64DataBC.getBuffer(); decodedDataCC.recycle(); // handle the edge case, so we don't have to worry about it later if(end - start == 0) { return; } int numberQuadruple = (end - start)/FOURBYTE; byte b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0; // Throw away anything not in base64Data int encodedIndex = 0; int dataIndex = start; char[] decodedData = null; { // this sizes the output array properly - rlw int lastData = end - start; // ignore the '=' padding while (base64Data[start+lastData-1] == PAD) { if (--lastData == 0) { return; } } decodedDataCC.allocate(lastData - numberQuadruple, -1); decodedDataCC.setEnd(lastData - numberQuadruple); decodedData = decodedDataCC.getBuffer(); } for (int i = 0; i < numberQuadruple; i++) { dataIndex = start + i * 4; marker0 = base64Data[dataIndex + 2]; marker1 = base64Data[dataIndex + 3]; b1 = base64Alphabet[base64Data[dataIndex]]; b2 = base64Alphabet[base64Data[dataIndex +1]]; if (marker0 != PAD && marker1 != PAD) { //No PAD e.g 3cQl b3 = base64Alphabet[ marker0 ]; b4 = base64Alphabet[ marker1 ]; decodedData[encodedIndex] = (char) (( b1 <<2 | b2>>4 ) & 0xff); decodedData[encodedIndex + 1] = (char) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff); decodedData[encodedIndex + 2] = (char) (( b3<<6 | b4 ) & 0xff); } else if (marker0 == PAD) { //Two PAD e.g. 3c[Pad][Pad] decodedData[encodedIndex] = (char) (( b1 <<2 | b2>>4 ) & 0xff); } else if (marker1 == PAD) { //One PAD e.g. 3cQ[Pad] b3 = base64Alphabet[ marker0 ]; decodedData[encodedIndex] = (char) (( b1 <<2 | b2>>4 ) & 0xff); decodedData[encodedIndex + 1] = (char) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff); } encodedIndex += 3; } }
Example 17
Source File: CoyoteAdapter.java From tomcatsrc with Apache License 2.0 | 4 votes |
/** * Extract the path parameters from the request. This assumes parameters are * of the form /path;name=value;name2=value2/ etc. Currently only really * interested in the session ID that will be in this form. Other parameters * can safely be ignored. * * @param req * @param request */ protected void parsePathParameters(org.apache.coyote.Request req, Request request) { // Process in bytes (this is default format so this is normally a NO-OP req.decodedURI().toBytes(); ByteChunk uriBC = req.decodedURI().getByteChunk(); int semicolon = uriBC.indexOf(';', 0); // What encoding to use? Some platforms, eg z/os, use a default // encoding that doesn't give the expected result so be explicit String enc = connector.getURIEncoding(); if (enc == null) { enc = "ISO-8859-1"; } Charset charset = null; try { charset = B2CConverter.getCharset(enc); } catch (UnsupportedEncodingException e1) { log.warn(sm.getString("coyoteAdapter.parsePathParam", enc)); } if (log.isDebugEnabled()) { log.debug(sm.getString("coyoteAdapter.debug", "uriBC", uriBC.toString())); log.debug(sm.getString("coyoteAdapter.debug", "semicolon", String.valueOf(semicolon))); log.debug(sm.getString("coyoteAdapter.debug", "enc", enc)); } while (semicolon > -1) { // Parse path param, and extract it from the decoded request URI int start = uriBC.getStart(); int end = uriBC.getEnd(); int pathParamStart = semicolon + 1; int pathParamEnd = ByteChunk.findBytes(uriBC.getBuffer(), start + pathParamStart, end, new byte[] {';', '/'}); String pv = null; if (pathParamEnd >= 0) { if (charset != null) { pv = new String(uriBC.getBuffer(), start + pathParamStart, pathParamEnd - pathParamStart, charset); } // Extract path param from decoded request URI byte[] buf = uriBC.getBuffer(); for (int i = 0; i < end - start - pathParamEnd; i++) { buf[start + semicolon + i] = buf[start + i + pathParamEnd]; } uriBC.setBytes(buf, start, end - start - pathParamEnd + semicolon); } else { if (charset != null) { pv = new String(uriBC.getBuffer(), start + pathParamStart, (end - start) - pathParamStart, charset); } uriBC.setEnd(start + semicolon); } if (log.isDebugEnabled()) { log.debug(sm.getString("coyoteAdapter.debug", "pathParamStart", String.valueOf(pathParamStart))); log.debug(sm.getString("coyoteAdapter.debug", "pathParamEnd", String.valueOf(pathParamEnd))); log.debug(sm.getString("coyoteAdapter.debug", "pv", pv)); } if (pv != null) { int equals = pv.indexOf('='); if (equals > -1) { String name = pv.substring(0, equals); String value = pv.substring(equals + 1); request.addPathParameter(name, value); if (log.isDebugEnabled()) { log.debug(sm.getString("coyoteAdapter.debug", "equals", String.valueOf(equals))); log.debug(sm.getString("coyoteAdapter.debug", "name", name)); log.debug(sm.getString("coyoteAdapter.debug", "value", value)); } } } semicolon = uriBC.indexOf(';', semicolon); } }
Example 18
Source File: CoyoteAdapter.java From Tomcat7.0.67 with Apache License 2.0 | 4 votes |
/** * Extract the path parameters from the request. This assumes parameters are * of the form /path;name=value;name2=value2/ etc. Currently only really * interested in the session ID that will be in this form. Other parameters * can safely be ignored. * * @param req * @param request */ protected void parsePathParameters(org.apache.coyote.Request req, Request request) { // Process in bytes (this is default format so this is normally a NO-OP req.decodedURI().toBytes(); ByteChunk uriBC = req.decodedURI().getByteChunk(); int semicolon = uriBC.indexOf(';', 0); // What encoding to use? Some platforms, eg z/os, use a default // encoding that doesn't give the expected result so be explicit String enc = connector.getURIEncoding(); if (enc == null) { enc = "ISO-8859-1"; } Charset charset = null; try { charset = B2CConverter.getCharset(enc); } catch (UnsupportedEncodingException e1) { log.warn(sm.getString("coyoteAdapter.parsePathParam", enc)); } if (log.isDebugEnabled()) { log.debug(sm.getString("coyoteAdapter.debug", "uriBC", uriBC.toString())); log.debug(sm.getString("coyoteAdapter.debug", "semicolon", String.valueOf(semicolon))); log.debug(sm.getString("coyoteAdapter.debug", "enc", enc)); } while (semicolon > -1) { // Parse path param, and extract it from the decoded request URI int start = uriBC.getStart(); int end = uriBC.getEnd(); int pathParamStart = semicolon + 1; int pathParamEnd = ByteChunk.findBytes(uriBC.getBuffer(), start + pathParamStart, end, new byte[] {';', '/'}); String pv = null; if (pathParamEnd >= 0) { if (charset != null) { pv = new String(uriBC.getBuffer(), start + pathParamStart, pathParamEnd - pathParamStart, charset); } // Extract path param from decoded request URI byte[] buf = uriBC.getBuffer(); for (int i = 0; i < end - start - pathParamEnd; i++) { buf[start + semicolon + i] = buf[start + i + pathParamEnd]; } uriBC.setBytes(buf, start, end - start - pathParamEnd + semicolon); } else { if (charset != null) { pv = new String(uriBC.getBuffer(), start + pathParamStart, (end - start) - pathParamStart, charset); } uriBC.setEnd(start + semicolon); } if (log.isDebugEnabled()) { log.debug(sm.getString("coyoteAdapter.debug", "pathParamStart", String.valueOf(pathParamStart))); log.debug(sm.getString("coyoteAdapter.debug", "pathParamEnd", String.valueOf(pathParamEnd))); log.debug(sm.getString("coyoteAdapter.debug", "pv", pv)); } if (pv != null) { int equals = pv.indexOf('='); if (equals > -1) { String name = pv.substring(0, equals); String value = pv.substring(equals + 1); request.addPathParameter(name, value); if (log.isDebugEnabled()) { log.debug(sm.getString("coyoteAdapter.debug", "equals", String.valueOf(equals))); log.debug(sm.getString("coyoteAdapter.debug", "name", name)); log.debug(sm.getString("coyoteAdapter.debug", "value", value)); } } } semicolon = uriBC.indexOf(';', semicolon); } }
Example 19
Source File: Base64.java From tomcatsrc with Apache License 2.0 | 4 votes |
/** * Decodes Base64 data into octets * * @param base64DataBC Byte array containing Base64 data * @param decodedDataCC The decoded data chars */ public static void decode( ByteChunk base64DataBC, CharChunk decodedDataCC) { int start = base64DataBC.getStart(); int end = base64DataBC.getEnd(); byte[] base64Data = base64DataBC.getBuffer(); decodedDataCC.recycle(); // handle the edge case, so we don't have to worry about it later if(end - start == 0) { return; } int numberQuadruple = (end - start)/FOURBYTE; byte b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0; // Throw away anything not in base64Data int encodedIndex = 0; int dataIndex = start; char[] decodedData = null; { // this sizes the output array properly - rlw int lastData = end - start; // ignore the '=' padding while (base64Data[start+lastData-1] == PAD) { if (--lastData == 0) { return; } } decodedDataCC.allocate(lastData - numberQuadruple, -1); decodedDataCC.setEnd(lastData - numberQuadruple); decodedData = decodedDataCC.getBuffer(); } for (int i = 0; i < numberQuadruple; i++) { dataIndex = start + i * 4; marker0 = base64Data[dataIndex + 2]; marker1 = base64Data[dataIndex + 3]; b1 = base64Alphabet[base64Data[dataIndex]]; b2 = base64Alphabet[base64Data[dataIndex +1]]; if (marker0 != PAD && marker1 != PAD) { //No PAD e.g 3cQl b3 = base64Alphabet[ marker0 ]; b4 = base64Alphabet[ marker1 ]; decodedData[encodedIndex] = (char) (( b1 <<2 | b2>>4 ) & 0xff); decodedData[encodedIndex + 1] = (char) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff); decodedData[encodedIndex + 2] = (char) (( b3<<6 | b4 ) & 0xff); } else if (marker0 == PAD) { //Two PAD e.g. 3c[Pad][Pad] decodedData[encodedIndex] = (char) (( b1 <<2 | b2>>4 ) & 0xff); } else if (marker1 == PAD) { //One PAD e.g. 3cQ[Pad] b3 = base64Alphabet[ marker0 ]; decodedData[encodedIndex] = (char) (( b1 <<2 | b2>>4 ) & 0xff); decodedData[encodedIndex + 1] = (char) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff); } encodedIndex += 3; } }
Example 20
Source File: Base64.java From tomcatsrc with Apache License 2.0 | 4 votes |
/** * Decodes Base64 data into octets * * @param base64DataBC Byte array containing Base64 data * @param decodedDataBC The decoded data bytes */ public static void decode( ByteChunk base64DataBC, ByteChunk decodedDataBC) { int start = base64DataBC.getStart(); int end = base64DataBC.getEnd(); byte[] base64Data = base64DataBC.getBuffer(); decodedDataBC.recycle(); // handle the edge case, so we don't have to worry about it later if(end - start == 0) { return; } int numberQuadruple = (end - start)/FOURBYTE; byte b1=0,b2=0,b3=0, b4=0, marker0=0, marker1=0; // Throw away anything not in base64Data int encodedIndex = 0; int dataIndex = start; byte[] decodedData = null; { // this sizes the output array properly - rlw int lastData = end - start; // ignore the '=' padding while (base64Data[start+lastData-1] == PAD) { if (--lastData == 0) { return; } } decodedDataBC.allocate(lastData - numberQuadruple, -1); decodedDataBC.setEnd(lastData - numberQuadruple); decodedData = decodedDataBC.getBuffer(); } for (int i = 0; i < numberQuadruple; i++) { dataIndex = start + i * 4; marker0 = base64Data[dataIndex + 2]; marker1 = base64Data[dataIndex + 3]; b1 = base64Alphabet[base64Data[dataIndex]]; b2 = base64Alphabet[base64Data[dataIndex +1]]; if (marker0 != PAD && marker1 != PAD) { //No PAD e.g 3cQl b3 = base64Alphabet[ marker0 ]; b4 = base64Alphabet[ marker1 ]; decodedData[encodedIndex] = (byte) (( b1 <<2 | b2>>4 ) & 0xff); decodedData[encodedIndex + 1] = (byte) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff); decodedData[encodedIndex + 2] = (byte) (( b3<<6 | b4 ) & 0xff); } else if (marker0 == PAD) { //Two PAD e.g. 3c[Pad][Pad] decodedData[encodedIndex] = (byte) (( b1 <<2 | b2>>4 ) & 0xff); } else if (marker1 == PAD) { //One PAD e.g. 3cQ[Pad] b3 = base64Alphabet[ marker0 ]; decodedData[encodedIndex] = (byte) (( b1 <<2 | b2>>4 ) & 0xff); decodedData[encodedIndex + 1] = (byte) ((((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) ) & 0xff); } encodedIndex += 3; } }