/** Converts a String into a byte array. */ protected static byte[] byteArrayFromString(String s) throws TextParseException { byte[] array = s.getBytes(); boolean escaped = false; boolean hasEscapes = false; for (int i = 0; i < array.length; i++) { if (array[i] == '\\') { hasEscapes = true; break; } } if (!hasEscapes) { if (array.length > 255) { throw new TextParseException("text string too long"); } return array; } ByteArrayOutputStream os = new ByteArrayOutputStream(); int digits = 0; int intval = 0; for (int i = 0; i < array.length; i++) { byte b = array[i]; if (escaped) { if (b >= '0' && b <= '9' && digits < 3) { digits++; intval *= 10; intval += (b - '0'); if (intval > 255) throw new TextParseException("bad escape"); if (digits < 3) continue; b = (byte) intval; } else if (digits > 0 && digits < 3) throw new TextParseException("bad escape"); os.write(b); escaped = false; } else if (array[i] == '\\') { escaped = true; digits = 0; intval = 0; } else os.write(array[i]); } if (digits > 0 && digits < 3) throw new TextParseException("bad escape"); array = os.toByteArray(); if (array.length > 255) { throw new TextParseException("text string too long"); } return os.toByteArray(); }
public static byte[] parse(String s) { if (s.equals("(null)")) { return null; } try { int n = s.length(); ByteArrayOutputStream out = new ByteArrayOutputStream(n / 3); StringReader r = new StringReader(s); while (true) { int b1 = nextNibble(r); if (b1 < 0) { break; } int b2 = nextNibble(r); if (b2 < 0) { throw new RuntimeException("Invalid string " + s); } int b = (b1 << 4) | b2; out.write(b); } return out.toByteArray(); } catch (IOException e) { throw new RuntimeException(e); } }
private Class<?> getClassFromStream( final InputStream stream, final String classname, final File container) throws IOException, SecurityException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); int bytesRead = -1; final byte[] buffer = new byte[8192]; while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) { baos.write(buffer, 0, bytesRead); } final byte[] classData = baos.toByteArray(); return this.defineClassFromData(container, classData, classname); }
public byte[] getBytes(String className) { try { Tracer.mark(); String realName = className.replace(".", "/"); realName += ".class"; JarEntry je = jf.getJarEntry(realName); InputStream is = jf.getInputStream(je); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buff = new byte[4096]; while (is.available() > 0) { int read = is.read(buff); baos.write(buff, 0, read); } is.close(); return baos.toByteArray(); } catch (Exception e) { } finally { Tracer.unmark(); } return null; }