Example usage for java.io DataInputStream readFully

List of usage examples for java.io DataInputStream readFully

Introduction

In this page you can find the example usage for java.io DataInputStream readFully.

Prototype

public final void readFully(byte b[]) throws IOException 

Source Link

Document

See the general contract of the readFully method of DataInput .

Usage

From source file:org.cloudata.core.commitlog.CommitLogClient.java

static List<TransactionData> readTxDataFrom(String tabletName, DataInputStream dis) throws IOException {
    List<TransactionData> txDataList = new ArrayList<TransactionData>();
    while (true) {
        int totalByteLength = 0;

        try {//www.  j ava 2 s. co  m
            totalByteLength = dis.readInt();
        } catch (EOFException e) {
            break;
        }

        if (totalByteLength <= 0) {
            LOG.warn("totalByteLength is smaller than zero[" + totalByteLength + "]!!, ignore this data,"
                    + tabletName);
            break;
        }

        if (totalByteLength >= 5 * 1024 * 1024) {
            LOG.warn("totalByteLength is large than 5MB[" + totalByteLength + "]!!, ignore this data,"
                    + tabletName);
            break;
        }
        byte[] bytes = new byte[totalByteLength];
        dis.readFully(bytes);
        txDataList.add(TransactionData.createFrom(bytes));
    }

    return txDataList;
}

From source file:nz.govt.natlib.ndha.wctdpsdepositor.extractor.XPathWctMetsExtractor.java

public void parseFile(File wctMets, FileArchiveBuilder fileBuilder) throws IOException {
    DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(wctMets)));
    byte[] buff = new byte[(int) wctMets.length()];
    dis.readFully(buff);
    dis.close();//  ww w .j  ava 2  s  .  com

    String xmlEscapedString = escapeXml(buff);
    parseFile(xmlEscapedString.getBytes(), wctMets.getName(), fileBuilder);
}

From source file:bixo.parser.SimpleParserTest.java

private FetchedDatum makeFetchedDatum(URL path) throws IOException {
    File file = new File(path.getFile());
    byte[] bytes = new byte[(int) file.length()];
    DataInputStream in = new DataInputStream(new FileInputStream(file));
    in.readFully(bytes);

    String url = path.toExternalForm().toString();
    FetchedDatum fetchedDatum = new FetchedDatum(url, url, System.currentTimeMillis(), new HttpHeaders(),
            new ContentBytes(bytes), "text/html", 0);
    return fetchedDatum;
}

From source file:org.trianacode.taskgraph.util.FileUtils.java

/**
 * Reads the file specified by the given Filename and puts it into a string. This uses DataInputStream to createTool
 * the file fully and then create a string which is a lot faster than reading a line at a time. Use this function
 * for speed.//from   w  w  w  . j  a va  2  s.  co  m
 */
public static String readFile(String filename) {
    String st = null;

    try {
        if (filename.indexOf("://") != -1) { // a network path
            BufferedReader br = createReader(correctURL(filename));
            String contents = readFile(br);
            closeReader(br);
            return contents;
        }

        File fi = new File(filename);
        long llen = fi.length();
        int len = (int) (llen & 0x7FFFFFFF);
        if (len != llen) {
            System.out.println("File too big to createTool into a single object");
            return null;
        }

        byte[] b = new byte[len];

        DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(fi)));
        dis.readFully(b);
        st = new String(b);
        dis.close();
    } catch (FileNotFoundException ee) {
        logger.warn("Couldn't find file " + filename + ":" + formatThrowable(ee));
        return null;
    } catch (IOException eee) {
        logger.warn("IO exception on file " + filename + ":" + formatThrowable(eee));
        return null;
    }

    return st;
}

From source file:org.carbondata.processing.dataprocessor.dataretention.CarbonDataRetentionUtil.java

/**
 * This API will scan the level file and return the surrogate key for the
 * valid member//  www.j a  v a 2  s  . co  m
 *
 * @param levelName
 * @throws ParseException
 */
public static Map<Integer, Integer> getSurrogateKeyForRetentionMember(CarbonFile memberFile, String levelName,
        String columnValue, String format, Map<Integer, Integer> mapOfSurrKeyAndAvailStatus) {
    DataInputStream inputStream = null;
    Date storeDateMember = null;
    Date columnValDateMember = null;
    DataInputStream inputStreamForMaxVal = null;
    try {
        columnValDateMember = convertToDateObjectFromStringVal(columnValue, format, true);
    } catch (ParseException e) {
        LOGGER.error(e, "Not able to get surrogate key for value : " + columnValue);
        return mapOfSurrKeyAndAvailStatus;
    }
    try {
        inputStream = FileFactory.getDataInputStream(memberFile.getPath(),
                FileFactory.getFileType(memberFile.getPath()));

        long currPosIndex = 0;
        long size = memberFile.getSize() - 4;
        int minVal = inputStream.readInt();
        int surrogateKeyIndex = minVal;
        currPosIndex += 4;
        //
        int current = 0;
        boolean enableEncoding = Boolean.valueOf(
                CarbonProperties.getInstance().getProperty(CarbonCommonConstants.ENABLE_BASE64_ENCODING,
                        CarbonCommonConstants.ENABLE_BASE64_ENCODING_DEFAULT));
        String memberName = null;
        while (currPosIndex < size) {
            int len = inputStream.readInt();
            currPosIndex += 4;
            byte[] rowBytes = new byte[len];
            inputStream.readFully(rowBytes);
            currPosIndex += len;
            if (enableEncoding) {
                memberName = new String(Base64.decodeBase64(rowBytes), Charset.defaultCharset());
            } else {
                memberName = new String(rowBytes, Charset.defaultCharset());
            }
            int surrogateVal = surrogateKeyIndex + current;
            current++;
            try {
                storeDateMember = convertToDateObjectFromStringVal(memberName, format, false);

            } catch (Exception e) {
                LOGGER.error(e, "Not able to get surrogate key for value : " + memberName);
                continue;
            }
            // means date1 is before date2
            if (null != columnValDateMember && null != storeDateMember) {
                if (storeDateMember.compareTo(columnValDateMember) < 0) {
                    mapOfSurrKeyAndAvailStatus.put(surrogateVal, surrogateVal);
                }
            }

        }

    } catch (IOException e) {
        LOGGER.error(e, "Not able to read level file for Populating Cache : " + memberFile.getName());

    } finally {
        CarbonUtil.closeStreams(inputStream);
        CarbonUtil.closeStreams(inputStreamForMaxVal);

    }

    return mapOfSurrKeyAndAvailStatus;
}

From source file:com.athena.chameleon.engine.utils.JaxbUtilsTest.java

private String fileToString(String file) {
    String result = null;//from  w w w .  jav  a  2s.c  om
    DataInputStream in = null;

    try {
        File f = new File(file);
        byte[] buffer = new byte[(int) f.length()];
        in = new DataInputStream(new FileInputStream(f));
        in.readFully(buffer);
        result = new String(buffer);
        IOUtils.closeQuietly(in);
    } catch (IOException e) {
        throw new RuntimeException("IO problem in fileToString", e);
    }

    return result;
}

From source file:com.google.gwt.dev.javac.CachedCompilationUnit.java

public static CachedCompilationUnit load(InputStream inputStream, JsProgram jsProgram) throws Exception {
    DataInputStream dis = new DataInputStream(new BufferedInputStream(inputStream));
    try {/*from ww  w.j av  a  2s.c o  m*/
        CachedCompilationUnit compilationUnit = new CachedCompilationUnit();
        // version
        long version = dis.readLong();
        if (version != CompilationUnitDiskCache.CACHE_VERSION) {
            return null;
        }
        // some simple stuff :)
        compilationUnit.m_lastModified = dis.readLong();
        compilationUnit.m_displayLocation = dis.readUTF();
        compilationUnit.m_typeName = dis.readUTF();
        compilationUnit.m_contentId = new ContentId(dis.readUTF());
        compilationUnit.m_isSuperSource = dis.readBoolean();
        // compiled classes
        {
            int size = dis.readInt();
            compilationUnit.m_compiledClasses = new ArrayList<CompiledClass>(size);
            for (int i = 0; i < size; ++i) {
                // internal name
                String internalName = dis.readUTF();
                // is local
                boolean isLocal = dis.readBoolean();
                // bytes
                int byteSize = dis.readInt();
                byte[] bytes = new byte[byteSize];
                dis.readFully(bytes);
                // enclosing class
                CompiledClass enclosingClass = null;
                String enclosingClassName = dis.readUTF();
                if (!StringUtils.isEmpty(enclosingClassName)) {
                    for (CompiledClass cc : compilationUnit.m_compiledClasses) {
                        if (enclosingClassName.equals(cc.getInternalName())) {
                            enclosingClass = cc;
                            break;
                        }
                    }
                }
                // some assertion
                if (!StringUtils.isEmpty(enclosingClassName) && enclosingClass == null) {
                    throw new IllegalStateException("Can't find the enclosing class \"" + enclosingClassName
                            + "\" for \"" + internalName + "\"");
                }
                // init unit
                CompiledClass cc = new CompiledClass(internalName, bytes, isLocal, enclosingClass);
                cc.initUnit(compilationUnit);
                compilationUnit.m_compiledClasses.add(cc);
            }
        }
        // dependencies
        {
            compilationUnit.m_dependencies = new HashSet<ContentId>();
            int size = dis.readInt();
            if (size > 0) {
                for (int i = 0; i < size; i++) {
                    compilationUnit.m_dependencies.add(new ContentId(dis.readUTF()));
                }
            }
        }
        // JSNI methods
        {
            compilationUnit.m_jsniMethods = new ArrayList<JsniMethod>();
            int size = dis.readInt();
            if (size > 0) {
                for (int i = 0; i < size; i++) {
                    String name = dis.readUTF();
                    int startPos = dis.readInt();
                    int endPos = dis.readInt();
                    int startLine = dis.readInt();
                    String source = dis.readUTF();
                    String fileName = compilationUnit.m_displayLocation;
                    SourceInfo jsInfo = SourceOrigin.create(startPos, endPos, startLine, fileName);
                    compilationUnit.m_jsniMethods
                            .add(JsniCollector.restoreJsniMethod(name, source, jsInfo, jsProgram));
                }
            }
        }
        // Method lookup
        {
            compilationUnit.m_methodArgs = MethodArgNamesLookup.load(dis);
        }
        return compilationUnit;
    } finally {
        IOUtils.closeQuietly(dis);
    }
}

From source file:ClassPath.java

/**
 * @param name//from w w w. ja  va  2s  . c  o m
 *          fully qualified file name, e.g. java/lang/String
 * @param suffix
 *          file name ends with suffix, e.g. .java
 * @return byte array for file on class path
 */
public byte[] getBytes(String name, String suffix) throws IOException {
    DataInputStream dis = null;
    try {
        InputStream is = getInputStream(name, suffix);
        if (is == null) {
            throw new IOException("Couldn't find: " + name + suffix);
        }
        dis = new DataInputStream(is);
        byte[] bytes = new byte[is.available()];
        dis.readFully(bytes);
        return bytes;
    } finally {
        if (dis != null) {
            dis.close();
        }
    }
}

From source file:org.apache.cassandra.db.ReadCommand.java

public ReadCommand deserialize(DataInputStream dis) throws IOException {
    String table = dis.readUTF();
    String key = dis.readUTF();/*from  w  w  w . java  2 s .  c  om*/
    String columnFamily_column = dis.readUTF();
    int start = dis.readInt();
    int count = dis.readInt();
    long sinceTimestamp = dis.readLong();
    boolean isDigest = dis.readBoolean();

    int size = dis.readInt();
    List<String> columns = new ArrayList<String>();
    for (int i = 0; i < size; ++i) {
        byte[] bytes = new byte[dis.readInt()];
        dis.readFully(bytes);
        columns.add(new String(bytes));
    }
    ReadCommand rm = null;
    if (columns.size() > 0) {
        rm = new ReadCommand(table, key, columnFamily_column, columns);
    } else if (sinceTimestamp > 0) {
        rm = new ReadCommand(table, key, columnFamily_column, sinceTimestamp);
    } else {
        rm = new ReadCommand(table, key, columnFamily_column, start, count);
    }
    rm.setDigestQuery(isDigest);
    return rm;
}

From source file:org.apache.fop.fonts.type1.PFBParser.java

private void parsePCFormat(PFBData pfb, DataInputStream din) throws IOException {
    int segmentHead;
    int segmentType;
    int bytesRead;

    //Read first segment
    segmentHead = din.readUnsignedByte();
    if (segmentHead != 128) {
        throw new IOException("Invalid file format. Expected ASCII 80hex");
    }/*from w  ww .jav a  2  s.  co m*/
    segmentType = din.readUnsignedByte(); //Read
    int len1 = swapInteger(din.readInt());
    byte[] headerSegment = new byte[len1];
    din.readFully(headerSegment);
    pfb.setHeaderSegment(headerSegment);

    //Read second segment
    segmentHead = din.readUnsignedByte();
    if (segmentHead != 128) {
        throw new IOException("Invalid file format. Expected ASCII 80hex");
    }
    segmentType = din.readUnsignedByte();
    int len2 = swapInteger(din.readInt());
    byte[] encryptedSegment = new byte[len2];
    din.readFully(encryptedSegment);
    pfb.setEncryptedSegment(encryptedSegment);

    //Read third segment
    segmentHead = din.readUnsignedByte();
    if (segmentHead != 128) {
        throw new IOException("Invalid file format. Expected ASCII 80hex");
    }
    segmentType = din.readUnsignedByte();
    int len3 = swapInteger(din.readInt());
    byte[] trailerSegment = new byte[len3];
    din.readFully(trailerSegment);
    pfb.setTrailerSegment(trailerSegment);

    //Read EOF indicator
    segmentHead = din.readUnsignedByte();
    if (segmentHead != 128) {
        throw new IOException("Invalid file format. Expected ASCII 80hex");
    }
    segmentType = din.readUnsignedByte();
    if (segmentType != 3) {
        throw new IOException("Expected segment type 3, but found: " + segmentType);
    }
}